<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>第 3 章 数据可视化 | 小蓝哥的知识荒原</title>
<meta name="author" content="李详">
<meta name="description" content="3.1 ggplot2绘图相关  3.1.1 ggplot2绘制桑基(冲击图) library(ggforce) library(ggsci) titanic &lt;- reshape2::melt(Titanic) # This is how we usually envision data for parallel sets #head(titanic) # Reshape for...">
<meta name="generator" content="bookdown 0.24 with bs4_book()">
<meta property="og:title" content="第 3 章 数据可视化 | 小蓝哥的知识荒原">
<meta property="og:type" content="book">
<meta property="og:image" content="https://raw.githubusercontent.com/DivadNojnarg/outstanding-shiny-ui/master/images/intro/crc-press-cover.svg">
<meta property="og:description" content="3.1 ggplot2绘图相关  3.1.1 ggplot2绘制桑基(冲击图) library(ggforce) library(ggsci) titanic &lt;- reshape2::melt(Titanic) # This is how we usually envision data for parallel sets #head(titanic) # Reshape for...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第 3 章 数据可视化 | 小蓝哥的知识荒原">
<meta name="twitter:description" content="3.1 ggplot2绘图相关  3.1.1 ggplot2绘制桑基(冲击图) library(ggforce) library(ggsci) titanic &lt;- reshape2::melt(Titanic) # This is how we usually envision data for parallel sets #head(titanic) # Reshape for...">
<meta name="twitter:image" content="https://raw.githubusercontent.com/DivadNojnarg/outstanding-shiny-ui/master/images/intro/crc-press-cover.svg">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/header-attrs-2.11/header-attrs.js"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.3.1/transition.js"></script><script src="libs/bs3compat-0.3.1/tabs.js"></script><script src="libs/bs3compat-0.3.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><link rel="stylesheet" href="css/style.css">
</head>
<body data-spy="scroll" data-target="#toc">

<div class="container-fluid">
<div class="row">
  <header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>

    <div class="d-flex align-items-start justify-content-between">
      <h1>
        <a href="index.html" title="">小蓝哥的知识荒原</a>
      </h1>
      <button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
    </div>

    <div id="main-nav" class="collapse-lg">
      <form role="search">
        <input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>

      <nav aria-label="Table of contents"><h2>Table of contents</h2>
        <ul class="book-toc list-unstyled">
<li><a class="" href="index.html">简介</a></li>
<li class="book-part">Part I: R</li>
<li><a class="" href="r-base.html"><span class="header-section-number">1</span> 语言基础</a></li>
<li><a class="" href="r-stat.html"><span class="header-section-number">2</span> 统计分析</a></li>
<li><a class="active" href="r-vis.html"><span class="header-section-number">3</span> 数据可视化</a></li>
<li><a class="" href="r-deve.html"><span class="header-section-number">4</span> R语言开发</a></li>
<li class="book-part">Part Ⅱ: Python</li>
<li><a class="" href="python-base.html"><span class="header-section-number">5</span> Python基础</a></li>
<li><a class="" href="python-stat.html"><span class="header-section-number">6</span> 数据统计分析</a></li>
<li><a class="" href="python-spider.html"><span class="header-section-number">7</span> Python爬虫</a></li>
<li><a class="" href="ai.html"><span class="header-section-number">8</span> 人工智能</a></li>
<li class="book-part">Part Ⅲ: 生物信息学</li>
<li><a class="" href="bio-base.html"><span class="header-section-number">9</span> 基础知识</a></li>
<li><a class="" href="bio-env.html"><span class="header-section-number">10</span> 环境搭建</a></li>
<li><a class="" href="genomics.html"><span class="header-section-number">11</span> 基因组学</a></li>
<li><a class="" href="rnaseq.html"><span class="header-section-number">12</span> RNA-Seq</a></li>
<li><a class="" href="meta.html"><span class="header-section-number">13</span> 代谢组学</a></li>
<li><a class="" href="pro.html"><span class="header-section-number">14</span> 蛋白质组</a></li>
<li><a class="" href="multi.html"><span class="header-section-number">15</span> 多组学</a></li>
<li class="book-part">Part Ⅳ: 文献阅读</li>
<li><a class="" href="patho.html"><span class="header-section-number">16</span> 植物病理学</a></li>
<li><a class="" href="liter-genomics.html"><span class="header-section-number">17</span> 基因组学</a></li>
<li><a class="" href="liter-rnaseq.html"><span class="header-section-number">18</span> RNA-Seq</a></li>
<li><a class="" href="liter-meta.html"><span class="header-section-number">19</span> 代谢组学</a></li>
<li><a class="" href="liter-pro.html"><span class="header-section-number">20</span> 蛋白质组</a></li>
<li class="book-part">Part Ⅴ: 文章发表</li>
<li><a class="" href="myarticle.html"><span class="header-section-number">21</span> 文章发表</a></li>
<li class="book-part">Part Ⅵ: 上课笔记</li>
<li><a class="" href="class.html"><span class="header-section-number">22</span> 上课笔记</a></li>
<li class="book-part">Part Ⅶ: 其他</li>
<li><a class="" href="other.html"><span class="header-section-number">23</span> 其他笔记</a></li>
<li><a class="" href="references.html">References</a></li>
</ul>

        <div class="book-extra">
          <p><a id="book-repo" href="https://github.com/lixiang117423/book4xiang">View book source <i class="fab fa-github"></i></a></p>
        </div>
      </nav>
</div>
  </header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="r-vis" class="section level1" number="3">
<h1>
<span class="header-section-number">第 3 章</span> 数据可视化<a class="anchor" aria-label="anchor" href="#r-vis"><i class="fas fa-link"></i></a>
</h1>
<div id="ggplot2绘图相关" class="section level2" number="3.1">
<h2>
<span class="header-section-number">3.1</span> <code>ggplot2</code>绘图相关<a class="anchor" aria-label="anchor" href="#ggplot2%E7%BB%98%E5%9B%BE%E7%9B%B8%E5%85%B3"><i class="fas fa-link"></i></a>
</h2>
<div id="ggplot2绘制桑基冲击图" class="section level3" number="3.1.1">
<h3>
<span class="header-section-number">3.1.1</span> <code>ggplot2</code>绘制桑基(冲击图)<a class="anchor" aria-label="anchor" href="#ggplot2%E7%BB%98%E5%88%B6%E6%A1%91%E5%9F%BA%E5%86%B2%E5%87%BB%E5%9B%BE"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggforce.data-imaginist.com">ggforce</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://nanx.me/ggsci/">ggsci</a></span><span class="op">)</span>
<span class="va">titanic</span> <span class="op">&lt;-</span> <span class="fu">reshape2</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/reshape2/man/melt.html">melt</a></span><span class="op">(</span><span class="va">Titanic</span><span class="op">)</span>
<span class="co"># This is how we usually envision data for parallel sets</span>
<span class="co">#head(titanic)</span>
<span class="co"># Reshape for putting the first 4 columns as axes in the plot</span>
<span class="va">titanic</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/gather_set_data.html">gather_set_data</a></span><span class="op">(</span><span class="va">titanic</span>, <span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">)</span>
<span class="co">#head(titanic)</span>
<span class="co"># Do the plotting</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">titanic</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span><span class="va">x</span>, id <span class="op">=</span> <span class="va">id</span>, split <span class="op">=</span> <span class="va">y</span>, value <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">Sex</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">0.3</span>, axis.width <span class="op">=</span> <span class="fl">0.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets_axes</a></span><span class="op">(</span>axis.width <span class="op">=</span> <span class="fl">0.2</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets_labels</a></span><span class="op">(</span>colour <span class="op">=</span> <span class="st">'white'</span>,angle <span class="op">=</span> <span class="fl">0</span>, size <span class="op">=</span> <span class="fl">6</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>expand <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">0</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://nanx.me/ggsci/reference/scale_aaas.html">scale_fill_aaas</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggtheme.html">theme_minimal</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>axis.text.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_blank</a></span><span class="op">(</span><span class="op">)</span>,
        axis.text.x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size <span class="op">=</span> <span class="fl">12</span><span class="op">)</span>,
        axis.title <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_blank</a></span><span class="op">(</span><span class="op">)</span>,
        panel.background <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_blank</a></span><span class="op">(</span><span class="op">)</span>,
        panel.grid <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_blank</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:ggplot4SankeyDiagram"></span>
<img src="3.R-vis_files/figure-html/ggplot4SankeyDiagram-1.png" alt="ggplot2绘制桑基图示例" width="672"><p class="caption">
图3.1: ggplot2绘制桑基图示例
</p>
</div>
</div>
<div id="其他tips" class="section level3" number="3.1.2">
<h3>
<span class="header-section-number">3.1.2</span> 其他Tips<a class="anchor" aria-label="anchor" href="#%E5%85%B6%E4%BB%96tips"><i class="fas fa-link"></i></a>
</h3>
<ul>
<li>限制Y轴范围：</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/coord_cartesian.html">coord_cartesian</a></span><span class="op">(</span>ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">5</span>, <span class="fl">22</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<ul>
<li>输出中文</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggtheme.html">theme_bw</a></span><span class="op">(</span>base_family <span class="op">=</span> <span class="st">"STKaiti"</span><span class="op">)</span></code></pre></div>
<ul>
<li>坐标轴文字角度</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">axis.text.x</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>angle <span class="op">=</span> <span class="fl">45</span>, hjust <span class="op">=</span> <span class="fl">1</span>, vjust <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></code></pre></div>
<ul>
<li>导出PPT格式</li>
</ul>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">export</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/export/man/graph2office.html">graph2ppt</a></span><span class="op">(</span><span class="va">p2</span>, width <span class="op">=</span> <span class="fl">6</span>, height <span class="op">=</span> <span class="fl">5</span>,
                  margins <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span>top <span class="op">=</span> <span class="fl">0.5</span>, right <span class="op">=</span> <span class="fl">0.5</span>, 
                                  bottom <span class="op">=</span> <span class="fl">0.5</span>, left<span class="op">=</span> <span class="fl">0.5</span><span class="op">)</span>, 
                  center <span class="op">=</span> <span class="cn">TRUE</span>,
                  file <span class="op">=</span> <span class="st">'../results/PPT/稻瘟病发病情况（2020）.ppt'</span>NA<span class="op">)</span>NA</code></pre></div>
<ul>
<li>渐变色填充</li>
</ul>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_gradient.html">scale_fill_gradient2</a></span><span class="op">(</span>low <span class="op">=</span> <span class="st">"#000080"</span>, mid <span class="op">=</span> <span class="st">"white"</span>, high <span class="op">=</span> <span class="st">"#B22222"</span><span class="op">)</span></code></pre></div>
<ul>
<li>坐标轴刻度长度</li>
</ul>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>axis.ticks.length.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0</span>,<span class="st">'mm'</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<ul>
<li>颠倒XY轴</li>
</ul>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/coord_flip.html">coord_flip</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="ggtree学习笔记" class="section level2" number="3.2">
<h2>
<span class="header-section-number">3.2</span> ggtree学习笔记<a class="anchor" aria-label="anchor" href="#ggtree%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"><i class="fas fa-link"></i></a>
</h2>
<div id="写在前面" class="section level3" number="3.2.1">
<h3>
<span class="header-section-number">3.2.1</span> 写在前面<a class="anchor" aria-label="anchor" href="#%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2"><i class="fas fa-link"></i></a>
</h3>
<p>Y叔的<code>ggtree</code><span class="citation">(<a href="references.html#ref-yu2017ggtree" role="doc-biblioref">Yu et al. 2017</a>)</span>毫无疑问是当前绘制美化系统发育树（下文简称<code>进化树</code>）的最佳工具，一直想学习，<!-- more -->但是都没有真真正正学习过，一是因为网上关于<code>gtree</code>的中文资源较少，另外一个原因是感觉到自己用不上，就没认真学习。春节在家，实在无聊，下定决心学一遍<code>ggtree</code>。下面的内容来自Y叔的博客，若有不当之处，恳请批评指正。</p>
</div>
<div id="关于进化树" class="section level3" number="3.2.2">
<h3>
<span class="header-section-number">3.2.2</span> 关于进化树<a class="anchor" aria-label="anchor" href="#%E5%85%B3%E4%BA%8E%E8%BF%9B%E5%8C%96%E6%A0%91"><i class="fas fa-link"></i></a>
</h3>
<div id="进化树怎么看" class="section level4" number="3.2.2.1">
<h4>
<span class="header-section-number">3.2.2.1</span> 进化树怎么看<a class="anchor" aria-label="anchor" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%80%8E%E4%B9%88%E7%9C%8B"><i class="fas fa-link"></i></a>
</h4>
<p>进化树展示的是进化关系，简单说就是亲缘关系，通常是使用物种的遗传序列（如DNA序列、氨基酸序列等）来构建的。进化树看起来和层次聚类很像，这两者有木有区别呢？Y叔在<code>统计之都</code>上的文章是这样描述的：</p>
<blockquote>
<p>层次聚类的侧重点在于分类，把距离近的聚在一起。而进化树的构建可以说也是一个聚类过程，但侧重点在于推测进化关系和进化距离 (evolutionary distance)。</p>
</blockquote>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/1.png"></div>
<p>上图展示的是典型的系统发育树。图中绿色、蓝色和红色的点都是<code>nodes</code>，也就是节点（个人理解），其中最外层绿色的点表示的是每个<code>sample</code>，这时绿色的点也叫<code>tips</code>；蓝色的点表示的是<code>父节点</code>，也就是从外往内两两<code>sample</code>的共有节点，可以理解成<code>祖先（ancestor）</code>，祖先之间还可以继续往上溯源，最终就汇集到红色的点，这个红色的点也就是<code>root</code>，需要注意的是有根树才具有<code>root</code>节点；横线叫做<code>分支(branches)</code>，这些横线表示的是进化变化（evolutionary changes），线的长短表示的是以时间或遗传变异为单位的进化变化。</p>
</div>
</div>
<div id="进化树数据格式" class="section level3" number="3.2.3">
<h3>
<span class="header-section-number">3.2.3</span> 进化树数据格式<a class="anchor" aria-label="anchor" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F"><i class="fas fa-link"></i></a>
</h3>
<p>进化树的数据格式有多种，常见的有<code>Newick</code>、<code>NEXUS</code>及<code>Phylip</code>。</p>
<div id="newick格式" class="section level4" number="3.2.3.1">
<h4>
<span class="header-section-number">3.2.3.1</span> Newick格式<a class="anchor" aria-label="anchor" href="#newick%E6%A0%BC%E5%BC%8F"><i class="fas fa-link"></i></a>
</h4>
<p><code>Newick</code>格式是最常见的使用最广泛的进化树数据格式。</p>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/2.png"></div>
<p>上图是一个标准的进化树图，其对应的<code>Newick</code>格式为：</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="r-vis.html#cb12-1" aria-hidden="true" tabindex="-1"></a>((t2<span class="sc">:</span><span class="fl">0.04</span>,t1<span class="sc">:</span><span class="fl">0.34</span>)<span class="sc">:</span><span class="fl">0.89</span>,(t5<span class="sc">:</span><span class="fl">0.37</span>,(t4<span class="sc">:</span><span class="fl">0.03</span>,t3<span class="sc">:</span><span class="fl">0.67</span>)<span class="sc">:</span><span class="fl">0.9</span>)<span class="sc">:</span><span class="fl">0.59</span>); </span></code></pre></div>
<p>数据格式和图是一一对应的，假如我们看图的时候是从外到内，那<code>t3</code>和<code>t4</code>是最近的，在数据中，<code>t3</code>和<code>t4</code>也是在一个括号里面的，数据的最小单位是一对<code>()</code>，就像剥洋葱那样顺着括号一层一层往外剥的时候，就能得到上图的那种样式。冒号后面对应的是横线上的“距离”，父节点的“距离”是两个“子节点”共有的，因此，需要表示“父节点”的“距离”时，需要将“距离”放在“子节点“的括号外。</p>
</div>
<div id="nexus格式" class="section level4" number="3.2.3.2">
<h4>
<span class="header-section-number">3.2.3.2</span> NEXUS格式<a class="anchor" aria-label="anchor" href="#nexus%E6%A0%BC%E5%BC%8F"><i class="fas fa-link"></i></a>
</h4>
<p><code>NEXUS</code>格式是<code>Newick</code>格式的拓展，以<code>blocks</code>为单位将进化树的元素分开。</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="r-vis.html#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">#NEXUS</span></span>
<span id="cb13-2"><a href="r-vis.html#cb13-2" aria-hidden="true" tabindex="-1"></a>[R<span class="sc">-</span>package APE, Wed Nov  <span class="dv">9</span> <span class="dv">11</span><span class="sc">:</span><span class="dv">46</span><span class="sc">:</span><span class="dv">32</span> <span class="dv">2016</span>]</span>
<span id="cb13-3"><a href="r-vis.html#cb13-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-4"><a href="r-vis.html#cb13-4" aria-hidden="true" tabindex="-1"></a>BEGIN TAXA;</span>
<span id="cb13-5"><a href="r-vis.html#cb13-5" aria-hidden="true" tabindex="-1"></a>    DIMENSIONS NTAX <span class="ot">=</span> <span class="dv">5</span>;</span>
<span id="cb13-6"><a href="r-vis.html#cb13-6" aria-hidden="true" tabindex="-1"></a>    TAXLABELS</span>
<span id="cb13-7"><a href="r-vis.html#cb13-7" aria-hidden="true" tabindex="-1"></a>        t5</span>
<span id="cb13-8"><a href="r-vis.html#cb13-8" aria-hidden="true" tabindex="-1"></a>        t4</span>
<span id="cb13-9"><a href="r-vis.html#cb13-9" aria-hidden="true" tabindex="-1"></a>        t1</span>
<span id="cb13-10"><a href="r-vis.html#cb13-10" aria-hidden="true" tabindex="-1"></a>        t2</span>
<span id="cb13-11"><a href="r-vis.html#cb13-11" aria-hidden="true" tabindex="-1"></a>        t3</span>
<span id="cb13-12"><a href="r-vis.html#cb13-12" aria-hidden="true" tabindex="-1"></a>    ;</span>
<span id="cb13-13"><a href="r-vis.html#cb13-13" aria-hidden="true" tabindex="-1"></a>END;</span>
<span id="cb13-14"><a href="r-vis.html#cb13-14" aria-hidden="true" tabindex="-1"></a>BEGIN TREES;</span>
<span id="cb13-15"><a href="r-vis.html#cb13-15" aria-hidden="true" tabindex="-1"></a>    TRANSLATE</span>
<span id="cb13-16"><a href="r-vis.html#cb13-16" aria-hidden="true" tabindex="-1"></a>        <span class="dv">1</span>   t5,</span>
<span id="cb13-17"><a href="r-vis.html#cb13-17" aria-hidden="true" tabindex="-1"></a>        <span class="dv">2</span>   t4,</span>
<span id="cb13-18"><a href="r-vis.html#cb13-18" aria-hidden="true" tabindex="-1"></a>        <span class="dv">3</span>   t1,</span>
<span id="cb13-19"><a href="r-vis.html#cb13-19" aria-hidden="true" tabindex="-1"></a>        <span class="dv">4</span>   t2,</span>
<span id="cb13-20"><a href="r-vis.html#cb13-20" aria-hidden="true" tabindex="-1"></a>        <span class="dv">5</span>   t3</span>
<span id="cb13-21"><a href="r-vis.html#cb13-21" aria-hidden="true" tabindex="-1"></a>    ;</span>
<span id="cb13-22"><a href="r-vis.html#cb13-22" aria-hidden="true" tabindex="-1"></a>    TREE <span class="sc">*</span> UNTITLED <span class="ot">=</span> [<span class="sc">&amp;</span>R] (<span class="dv">1</span><span class="sc">:</span><span class="fl">0.89</span>,((<span class="dv">2</span><span class="sc">:</span><span class="fl">0.59</span>,<span class="dv">3</span><span class="sc">:</span><span class="fl">0.37</span>)<span class="sc">:</span><span class="fl">0.34</span>,</span>
<span id="cb13-23"><a href="r-vis.html#cb13-23" aria-hidden="true" tabindex="-1"></a>    (<span class="dv">4</span><span class="sc">:</span><span class="fl">0.03</span>,<span class="dv">5</span><span class="sc">:</span><span class="fl">0.67</span>)<span class="sc">:</span><span class="fl">0.9</span>)<span class="sc">:</span><span class="fl">0.04</span>);</span>
<span id="cb13-24"><a href="r-vis.html#cb13-24" aria-hidden="true" tabindex="-1"></a>END;</span></code></pre></div>
</div>
<div id="其他格式" class="section level4" number="3.2.3.3">
<h4>
<span class="header-section-number">3.2.3.3</span> 其他格式<a class="anchor" aria-label="anchor" href="#%E5%85%B6%E4%BB%96%E6%A0%BC%E5%BC%8F"><i class="fas fa-link"></i></a>
</h4>
<p>进化树的存储格式还有很多种，更多请参照Y叔的博客<span class="math inline">\(^{[4]}\)</span>。</p>
</div>
</div>
<div id="进化树数据处理" class="section level3" number="3.2.4">
<h3>
<span class="header-section-number">3.2.4</span> 进化树数据处理<a class="anchor" aria-label="anchor" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86"><i class="fas fa-link"></i></a>
</h3>
<p>进化树数据可以使用<code>treeio</code><span class="math inline">\(^{[5]}\)</span>这个包进行合并等操作，然后可以将其他信息利用<code>tidytree</code><span class="math inline">\(^{[6]}\)</span>这个包将树文件转换成R里面常见的数据框格式，这种格式也可以再次转化成树文件，利用<code>ggtree</code>进行可视化。</p>
<div id="phylo对象" class="section level4" number="3.2.4.1">
<h4>
<span class="header-section-number">3.2.4.1</span> <code>phylo</code>对象<a class="anchor" aria-label="anchor" href="#phylo%E5%AF%B9%E8%B1%A1"><i class="fas fa-link"></i></a>
</h4>
<p><code>phylo</code>格式是R包<code>ape</code>支持的格式，在R中的很多包都依赖于这种格式。<code>tidytree</code>中的函数<code>as_tibble</code>可以将<code>phylo</code>转换成数据框，此时的数据框是个<code>tbl_tree</code>对象。</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="http://ape-package.ird.fr/">ape</a></span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2017</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">4</span><span class="op">)</span>
<span class="va">tree</span></code></pre></div>
<p>此时得到的是<code>phylo</code>对象：</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## </span>
<span class="co">## Phylogenetic tree with 4 tips and 3 internal nodes.</span>
<span class="co">## </span>
<span class="co">## Tip labels:</span>
<span class="co">##   t4, t1, t3, t2</span>
<span class="co">## </span>
<span class="co">## Rooted; includes branch lengths.</span></code></pre></div>
<p>现在将其转化成数据框：</p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span>
<span class="va">x</span></code></pre></div>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## # A tibble: 7 x 4</span>
<span class="co">##   parent  node branch.length label</span>
<span class="co">##    &lt;int&gt; &lt;int&gt;         &lt;dbl&gt; &lt;chr&gt;</span>
<span class="co">## 1      5     1       0.435   t4   </span>
<span class="co">## 2      7     2       0.674   t1   </span>
<span class="co">## 3      7     3       0.00202 t3   </span>
<span class="co">## 4      6     4       0.0251  t2   </span>
<span class="co">## 5      5     5      NA       &lt;NA&gt; </span>
<span class="co">## 6      5     6       0.472   &lt;NA&gt; </span>
<span class="co">## 7      6     7       0.274   &lt;NA&gt;</span></code></pre></div>
<p>看图更直观：</p>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/3.png"></div>
<p>可以清楚地看到进化树的全部信息，包括父节点、节点、分支长度及<code>tips</code>的标签等。</p>
<p>使用<code><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo(x)</a></code>就能将数据框转换成<code>phylo</code>:</p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>
<span class="co">## </span>
<span class="co">## Phylogenetic tree with 4 tips and 3 internal nodes.</span>
<span class="co">## </span>
<span class="co">## Tip labels:</span>
<span class="co">##   t4, t1, t3, t2</span>
<span class="co">## </span>
<span class="co">## Rooted; includes branch lengths.</span></code></pre></div>
<p>如果此时我们需要添加信息的话，在原始文件上添加信息是比较麻烦的，但是可以团购先构建数据框，然后将两个数据框<code>join</code>在一起就可以了：</p>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="st">'t'</span>, <span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">)</span>,
            trait <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">4</span><span class="op">)</span><span class="op">)</span>

<span class="va">y</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">x</span>, <span class="va">d</span>, by <span class="op">=</span> <span class="st">'label'</span><span class="op">)</span>
<span class="va">y</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/4.png"></div>
</div>
<div id="treedata对象" class="section level4" number="3.2.4.2">
<h4>
<span class="header-section-number">3.2.4.2</span> <code>treedata</code>对象<a class="anchor" aria-label="anchor" href="#treedata%E5%AF%B9%E8%B1%A1"><i class="fas fa-link"></i></a>
</h4>
<p><code>tidytree</code>默认的格式是<code>treedata</code>，函数<code>as.treedata</code>可以将前面的数据框转换成<code>treedata</code>对象：</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/as.treedata.html">as.treedata</a></span><span class="op">(</span><span class="va">y</span><span class="op">)</span></code></pre></div>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## 'treedata' S4 object'.</span>
<span class="co">## </span>
<span class="co">## ...@ phylo: </span>
<span class="co">## Phylogenetic tree with 4 tips and 3 internal nodes.</span>
<span class="co">## </span>
<span class="co">## Tip labels:</span>
<span class="co">##   t4, t1, t3, t2</span>
<span class="co">## </span>
<span class="co">## Rooted; includes branch lengths.</span>
<span class="co">## </span>
<span class="co">## with the following features available:</span>
<span class="co">##  'trait'.</span></code></pre></div>
<p>同样也可以通过<code>as_tibble</code>将<code>treedata</code>转换成数据框格式：</p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">y</span> <span class="op">%&gt;%</span> <span class="va">as.treedata</span> <span class="op">%&gt;%</span> <span class="va">as_tibble</span></code></pre></div>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## # A tibble: 7 x 5</span>
<span class="co">##   parent  node branch.length label  trait</span>
<span class="co">##    &lt;int&gt; &lt;int&gt;         &lt;dbl&gt; &lt;chr&gt;  &lt;dbl&gt;</span>
<span class="co">## 1      5     1       0.435   t4     0.943</span>
<span class="co">## 2      7     2       0.674   t1    -0.171</span>
<span class="co">## 3      7     3       0.00202 t3     0.570</span>
<span class="co">## 4      6     4       0.0251  t2    -0.283</span>
<span class="co">## 5      5     5      NA       &lt;NA&gt;  NA    </span>
<span class="co">## 6      5     6       0.472   &lt;NA&gt;  NA    </span>
<span class="co">## 7      6     7       0.274   &lt;NA&gt;  NA</span></code></pre></div>
</div>
<div id="树文件融合" class="section level4" number="3.2.4.3">
<h4>
<span class="header-section-number">3.2.4.3</span> 树文件融合<a class="anchor" aria-label="anchor" href="#%E6%A0%91%E6%96%87%E4%BB%B6%E8%9E%8D%E5%90%88"><i class="fas fa-link"></i></a>
</h4>
<p><code>treeio</code>同的函数<code><a href="https://docs.ropensci.org/treeio/reference/merge_tree.html">merge_tree()</a></code>可以对多个树文件进行合并，原理是以<code>node/branches</code>为`key进行数据合并，如：</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="va">beast_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/MCC_FluA_H3.tree"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">rst_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/rst"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">mlc_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/mlc"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">beast_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.beast</a></span><span class="op">(</span><span class="va">beast_file</span><span class="op">)</span>
<span class="va">codeml_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.codeml.html">read.codeml</a></span><span class="op">(</span><span class="va">rst_file</span>, <span class="va">mlc_file</span><span class="op">)</span>

<span class="va">merged_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/merge_tree.html">merge_tree</a></span><span class="op">(</span><span class="va">beast_tree</span>, <span class="va">codeml_tree</span><span class="op">)</span>
<span class="va">merged_tree</span></code></pre></div>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## 'treedata' S4 object that stored information of</span>
<span class="co">##  '/home/ygc/R/library/ggtree/examples/MCC_FluA_H3.tree',</span>
<span class="co">##  '/home/ygc/R/library/ggtree/examples/rst',</span>
<span class="co">##  '/home/ygc/R/library/ggtree/examples/mlc'.</span>
<span class="co">## </span>
<span class="co">## ...@ phylo: </span>
<span class="co">## Phylogenetic tree with 76 tips and 75 internal nodes.</span>
<span class="co">## </span>
<span class="co">## Tip labels:</span>
<span class="co">##   A/Hokkaido/30-1-a/2013, A/New_York/334/2004, A/New_York/463/2005, A/New_York/452/1999, A/New_York/238/2005, A/New_York/523/1998, ...</span>
<span class="co">## </span>
<span class="co">## Rooted; includes branch lengths.</span>
<span class="co">## </span>
<span class="co">## with the following features available:</span>
<span class="co">##  'height',   'height_0.95_HPD',  'height_median',</span>
<span class="co">##  'height_range', 'length',   'length_0.95_HPD',</span>
<span class="co">##  'length_median',    'length_range', 'posterior',    'rate',</span>
<span class="co">##  'rate_0.95_HPD',    'rate_median',  'rate_range',   'subs',</span>
<span class="co">##  'AA_subs',  't',    'N',    'S',    'dN_vs_dS', 'dN',   'dS',   'N_x_dN',</span>
<span class="co">##  'S_x_dS'.</span></code></pre></div>
<p>合并后的树文件，除开<code>node</code>和<code>branches</code>外，其余的所有附加信息都变成了变量。</p>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/5.png"></div>
<p>既然是个数据框，那就可以对这些数据进行可视化：</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span>
<span class="va">df</span> <span class="op">&lt;-</span> <span class="va">merged_tree</span> <span class="op">%&gt;%</span> 
  <span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><span class="op">(</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="va">dN_vs_dS</span>, <span class="va">dN</span>, <span class="va">dS</span>, <span class="va">rate</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu"><a href="https://rdrr.io/r/base/subset.html">subset</a></span><span class="op">(</span><span class="va">dN_vs_dS</span> <span class="op">&gt;=</span><span class="fl">0</span> <span class="op">&amp;</span> <span class="va">dN_vs_dS</span> <span class="op">&lt;=</span> <span class="fl">1.5</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/gather.html">gather</a></span><span class="op">(</span><span class="va">type</span>, <span class="va">value</span>, <span class="va">dN_vs_dS</span><span class="op">:</span><span class="va">dS</span><span class="op">)</span>
<span class="va">df</span><span class="op">$</span><span class="va">type</span><span class="op">[</span><span class="va">df</span><span class="op">$</span><span class="va">type</span> <span class="op">==</span> <span class="st">'dN_vs_dS'</span><span class="op">]</span> <span class="op">&lt;-</span> <span class="st">'dN/dS'</span>
<span class="va">df</span><span class="op">$</span><span class="va">type</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">df</span><span class="op">$</span><span class="va">type</span>, levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"dN/dS"</span>, <span class="st">"dN"</span>, <span class="st">"dS"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">df</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span><span class="va">rate</span>, <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_hex.html">geom_hex</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/facet_wrap.html">facet_wrap</a></span><span class="op">(</span><span class="op">~</span><span class="va">type</span>, scale<span class="op">=</span><span class="st">'free_y'</span><span class="op">)</span> </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/6.png"></div>
<p>还可以利用该函数比较不同软件的分析结果：</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">phylo</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">beast_tree</span><span class="op">)</span>
<span class="va">N</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/getNodeNum.html">Nnode2</a></span><span class="op">(</span><span class="va">phylo</span><span class="op">)</span>
<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="va">N</span>, fake_trait <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="va">N</span><span class="op">)</span>, another_trait <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Uniform.html">runif</a></span><span class="op">(</span><span class="va">N</span><span class="op">)</span><span class="op">)</span>
<span class="va">fake_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/treedata.html">treedata</a></span><span class="op">(</span>phylo <span class="op">=</span> <span class="va">phylo</span>, data <span class="op">=</span> <span class="va">d</span><span class="op">)</span>
<span class="va">triple_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/merge_tree.html">merge_tree</a></span><span class="op">(</span><span class="va">merged_tree</span>, <span class="va">fake_tree</span><span class="op">)</span>
<span class="va">triple_tree</span></code></pre></div>
</div>
<div id="链接外部数据" class="section level4" number="3.2.4.4">
<h4>
<span class="header-section-number">3.2.4.4</span> 链接外部数据<a class="anchor" aria-label="anchor" href="#%E9%93%BE%E6%8E%A5%E5%A4%96%E9%83%A8%E6%95%B0%E6%8D%AE"><i class="fas fa-link"></i></a>
</h4>
<p>进化树能够展示的东西不仅仅是进化关系，还可以增添许多信息，如基因表达量啥的。<code>treeio</code>的函数<code><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join()</a></code>能够通过<code>node</code>或<code>tips</code>进行数据融合：</p>
<ul>
<li>通过<code>node</code>：</li>
</ul>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="http://ape-package.ird.fr/">ape</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">woodmouse</span><span class="op">)</span>
<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/dist.dna.html">dist.dna</a></span><span class="op">(</span><span class="va">woodmouse</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/nj.html">nj</a></span><span class="op">(</span><span class="va">d</span><span class="op">)</span>
<span class="va">bp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/boot.phylo.html">boot.phylo</a></span><span class="op">(</span><span class="va">tr</span>, 
                 <span class="va">woodmouse</span>, 
                 <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/nj.html">nj</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/dist.dna.html">dist.dna</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>

<span class="va">bp2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/summary.phylo.html">Nnode</a></span><span class="op">(</span><span class="va">tr</span><span class="op">)</span> <span class="op">+</span> <span class="co"># 计算父节点数</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ape/man/summary.phylo.html">Ntip</a></span><span class="op">(</span><span class="va">tr</span><span class="op">)</span>, <span class="co"># 计算tip数</span>NA<span class="op">=</span> <span class="va">bp</span><span class="op">)</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">tr</span>, <span class="va">bp2</span>, by<span class="op">=</span><span class="st">"node"</span><span class="op">)</span></code></pre></div>
<ul>
<li>通过<code>tips</code>：</li>
</ul>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/BEAST"</span>, <span class="st">"beast_mcc.tree"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">beast</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.beast</a></span><span class="op">(</span><span class="va">file</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">beast</span><span class="op">)</span><span class="op">$</span><span class="va">tip.label</span>, trait <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/summary.phylo.html">Ntip</a></span><span class="op">(</span><span class="va">beast</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">beast</span>, <span class="va">x</span>, by<span class="op">=</span><span class="st">"label"</span><span class="op">)</span></code></pre></div>
</div>
<div id="如何找到想要的node" class="section level4" number="3.2.4.5">
<h4>
<span class="header-section-number">3.2.4.5</span> 如何找到想要的<code>node</code><a class="anchor" aria-label="anchor" href="#%E5%A6%82%E4%BD%95%E6%89%BE%E5%88%B0%E6%83%B3%E8%A6%81%E7%9A%84node"><i class="fas fa-link"></i></a>
</h4>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">1</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span>
<span class="va">p</span>  <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_label</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">branch</span>, label<span class="op">=</span><span class="va">node</span><span class="op">)</span><span class="op">)</span><span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">12</span>, fill<span class="op">=</span><span class="st">"steelblue"</span>, alpha<span class="op">=</span><span class="fl">.6</span><span class="op">)</span></code></pre></div>
</div>
<div id="分组" class="section level4" number="3.2.4.6">
<h4>
<span class="header-section-number">3.2.4.6</span> 分组<a class="anchor" aria-label="anchor" href="#%E5%88%86%E7%BB%84"><i class="fas fa-link"></i></a>
</h4>
<p><code>treeio</code>中的函数 <code><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU()</a></code> 和 <code><a href="https://rdrr.io/pkg/tidytree/man/groupClade.html">groupClade()</a></code> 可以用于分组。</p>
<div id="groupclade" class="section level5" number="3.2.4.6.1">
<h5>
<span class="header-section-number">3.2.4.6.1</span> <code>groupClade</code><a class="anchor" aria-label="anchor" href="#groupclade"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">nwk</span> <span class="op">&lt;-</span> <span class="st">'(((((((A:4,B:4):6,C:5):8,D:6):3,E:21):10,((F:4,G:12):14,H:8):13):13,((I:5,J:2):30,(K:11,L:11):2):17):4,M:56);'</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text<span class="op">=</span><span class="va">nwk</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupClade.html">groupClade</a></span><span class="op">(</span><span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">17</span>, <span class="fl">21</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## # A tibble: 25 x 5</span>
<span class="co">##    parent  node branch.length label group</span>
<span class="co">##     &lt;int&gt; &lt;int&gt;         &lt;dbl&gt; &lt;chr&gt; &lt;fct&gt;</span>
<span class="co">##  1     20     1             4 A     1    </span>
<span class="co">##  2     20     2             4 B     1    </span>
<span class="co">##  3     19     3             5 C     1    </span>
<span class="co">##  4     18     4             6 D     1    </span>
<span class="co">##  5     17     5            21 E     1    </span>
<span class="co">##  6     22     6             4 F     2    </span>
<span class="co">##  7     22     7            12 G     2    </span>
<span class="co">##  8     21     8             8 H     2    </span>
<span class="co">##  9     24     9             5 I     0    </span>
<span class="co">## 10     24    10             2 J     0    </span>
<span class="co">## # … with 15 more rows</span>NA</code></pre></div>
<p>以<code>node17</code>和<code>node21</code>为界将<code>clade</code>进行分组。这两个函数可以作用于<code>tbl_tree</code>、<code>phylo</code> 、 <code>treedata</code>及 <code>ggtree</code> 这些对象。</p>
</div>
<div id="groupotu" class="section level5" number="3.2.4.6.2">
<h5>
<span class="header-section-number">3.2.4.6.2</span> <code>groupOTU</code><a class="anchor" aria-label="anchor" href="#groupotu"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2017</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">4</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><span class="op">(</span><span class="va">tr</span><span class="op">)</span>
<span class="co">## 输入的节点可以是节点ID，也可以是Label</span>NA<span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">x</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">'t1'</span>, <span class="st">'t4'</span><span class="op">)</span>, group_name <span class="op">=</span> <span class="st">"fake_group"</span><span class="op">)</span></code></pre></div>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## # A tibble: 7 x 5</span>
<span class="co">##   parent  node branch.length label fake_group</span>
<span class="co">##    &lt;int&gt; &lt;int&gt;         &lt;dbl&gt; &lt;chr&gt; &lt;fct&gt;     </span>
<span class="co">## 1      5     1       0.435   t4    1         </span>
<span class="co">## 2      7     2       0.674   t1    1         </span>
<span class="co">## 3      7     3       0.00202 t3    0         </span>
<span class="co">## 4      6     4       0.0251  t2    0         </span>
<span class="co">## 5      5     5      NA       &lt;NA&gt;  1         </span>
<span class="co">## 6      5     6       0.472   &lt;NA&gt;  1         </span>
<span class="co">## 7      6     7       0.274   &lt;NA&gt;  1</span></code></pre></div>
<p>更常见的分组方法是直接命名分组：</p>
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">cls</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>c1<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>, <span class="st">"D"</span>, <span class="st">"E"</span><span class="op">)</span>,
            c2<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"F"</span>, <span class="st">"G"</span>, <span class="st">"H"</span><span class="op">)</span>,
            c3<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"L"</span>, <span class="st">"K"</span>, <span class="st">"I"</span>, <span class="st">"J"</span><span class="op">)</span>,
            c4<span class="op">=</span><span class="st">"M"</span><span class="op">)</span>

<span class="fu"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">cls</span><span class="op">)</span></code></pre></div>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## # A tibble: 25 x 5</span>
<span class="co">##    parent  node branch.length label group</span>
<span class="co">##     &lt;int&gt; &lt;int&gt;         &lt;dbl&gt; &lt;chr&gt; &lt;fct&gt;</span>
<span class="co">##  1     20     1             4 A     c1   </span>
<span class="co">##  2     20     2             4 B     c1   </span>
<span class="co">##  3     19     3             5 C     c1   </span>
<span class="co">##  4     18     4             6 D     c1   </span>
<span class="co">##  5     17     5            21 E     c1   </span>
<span class="co">##  6     22     6             4 F     c2   </span>
<span class="co">##  7     22     7            12 G     c2   </span>
<span class="co">##  8     21     8             8 H     c2   </span>
<span class="co">##  9     24     9             5 I     c3   </span>
<span class="co">## 10     24    10             2 J     c3   </span>
<span class="co">## # … with 15 more rows</span>NA</code></pre></div>
</div>
<div id="重新标准化分支" class="section level5" number="3.2.4.6.3">
<h5>
<span class="header-section-number">3.2.4.6.3</span> 重新标准化分支<a class="anchor" aria-label="anchor" href="#%E9%87%8D%E6%96%B0%E6%A0%87%E5%87%86%E5%8C%96%E5%88%86%E6%94%AF"><i class="fas fa-link"></i></a>
</h5>
<p>不同的进化树可以进行合并，原始的分支长度可能单位不一样，这时候就可以用合并后的其他参数对分支长度进行标准化。</p>
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">merged_tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="fu"><a href="https://docs.ropensci.org/treeio/reference/rescale_tree.html">rescale_tree</a></span><span class="op">(</span><span class="va">merged_tree</span>, <span class="st">'dN'</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">p3</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="fu"><a href="https://docs.ropensci.org/treeio/reference/rescale_tree.html">rescale_tree</a></span><span class="op">(</span><span class="va">merged_tree</span>, <span class="st">'rate'</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>, <span class="va">p3</span>, ncol<span class="op">=</span><span class="fl">3</span>, labels <span class="op">=</span> <span class="va">LETTERS</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">3</span><span class="op">]</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/7.png"></div>
<p>从上图可以看到的是，三个图的“横坐标”范围不一样，第一个图是原始的分支单位，第二个是以<code>dN</code>进行标准化的结果，第三个是以<code>rate</code>进行标准化的结果。</p>
</div>
</div>
<div id="子集操作" class="section level4" number="3.2.4.7">
<h4>
<span class="header-section-number">3.2.4.7</span> 子集操作<a class="anchor" aria-label="anchor" href="#%E5%AD%90%E9%9B%86%E6%93%8D%E4%BD%9C"><i class="fas fa-link"></i></a>
</h4>
<div id="从树中除去tips" class="section level5" number="3.2.4.7.1">
<h5>
<span class="header-section-number">3.2.4.7.1</span> 从树中除去<code>tips</code><a class="anchor" aria-label="anchor" href="#%E4%BB%8E%E6%A0%91%E4%B8%AD%E9%99%A4%E5%8E%BBtips"><i class="fas fa-link"></i></a>
</h5>
<p>有些时候出于某些原因（如序列质量、组装质量、比对错误等），我们会将某些<code>tips</code>（样本）从树里面剔除，函数<code><a href="https://docs.ropensci.org/treeio/reference/drop.tip-methods.html">drop.tip()</a></code>可以实现这一功能：</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">f</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/NHX"</span>, <span class="st">"phyldog.nhx"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">nhx</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.nhx.html">read.nhx</a></span><span class="op">(</span><span class="va">f</span><span class="op">)</span>
<span class="va">to_drop</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Physonect_sp_@2066767"</span>,
             <span class="st">"Lychnagalma_utricularia@2253871"</span>,
             <span class="st">"Kephyes_ovata@2606431"</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">nhx</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color <span class="op">=</span> <span class="va">label</span> <span class="op">%in%</span> <span class="va">to_drop</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">0.8</span><span class="op">)</span>

<span class="va">nhx_reduced</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/drop.tip.html">drop.tip</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">nhx</span><span class="op">)</span>, <span class="va">to_drop</span><span class="op">)</span> <span class="co"># 发现需要转换成phylo对象才行</span>NA<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">nhx_reduced</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">0.8</span><span class="op">)</span>  
<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>, ncol<span class="op">=</span><span class="fl">2</span>, labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/8.png"></div>
</div>
<div id="根据tips取子集" class="section level5" number="3.2.4.7.2">
<h5>
<span class="header-section-number">3.2.4.7.2</span> 根据<code>tips</code>取子集<a class="anchor" aria-label="anchor" href="#%E6%A0%B9%E6%8D%AEtips%E5%8F%96%E5%AD%90%E9%9B%86"><i class="fas fa-link"></i></a>
</h5>
<p>如果一个进化树很复杂的话，要看我们感兴趣的部分就很难，这时候就需要将我们感兴趣的部分提取出来。<code>treeio</code>中的函数<code><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset()</a></code>能够完成这一功能，即使是提取出来的子集，结构还是和原来的一样，不会发生变化。</p>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">beast_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/MCC_FluA_H3.tree"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">beast_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.beast</a></span><span class="op">(</span><span class="va">beast_file</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>  
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'原始树'</span><span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">40</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">tree2</span> <span class="op">=</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset</a></span><span class="op">(</span><span class="va">beast_tree</span>, <span class="st">"A/Swine/HK/168/2012"</span>, levels_back<span class="op">=</span><span class="fl">4</span><span class="op">)</span>  
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'取子集'</span><span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">4</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span> 

<span class="va">p3</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>hjust <span class="op">=</span> <span class="op">-</span><span class="fl">.1</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">5</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_point.html">geom_point</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">rate</span><span class="op">)</span>, shape <span class="op">=</span> <span class="fl">21</span>, size <span class="op">=</span> <span class="fl">4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'用rate这个变量控制颜色'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span>, guide <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_colour_continuous.html">scale_fill_continuous</a></span><span class="op">(</span>low <span class="op">=</span> <span class="st">'blue'</span>, high <span class="op">=</span> <span class="st">'red'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">'right'</span><span class="op">)</span>


<span class="va">p4</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span>, 
             root.position <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span><span class="op">$</span><span class="va">root.edge</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">18</span>, <span class="fl">24</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'添加根节点但不显示'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p5</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span>, 
             root.position <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/as.phylo.html">as.phylo</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span><span class="op">$</span><span class="va">root.edge</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_rootedge.html">geom_rootedge</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">40</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'添加根节点且显示'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>

<span class="fu">plot_grid</span><span class="op">(</span><span class="va">p2</span>, <span class="va">p3</span>, <span class="va">p4</span>, <span class="va">p5</span>, ncol<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">plot_grid</span><span class="op">(</span><span class="va">p1</span>, <span class="va">.</span>, ncol<span class="op">=</span><span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/9.png"></div>
</div>
<div id="根据内部节点编号取子集" class="section level5" number="3.2.4.7.3">
<h5>
<span class="header-section-number">3.2.4.7.3</span> 根据内部节点编号取子集<a class="anchor" aria-label="anchor" href="#%E6%A0%B9%E6%8D%AE%E5%86%85%E9%83%A8%E8%8A%82%E7%82%B9%E7%BC%96%E5%8F%B7%E5%8F%96%E5%AD%90%E9%9B%86"><i class="fas fa-link"></i></a>
</h5>
<p>如果我们对特定的进化分支（clade）感兴趣，那也可以通过<code><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset()</a></code>函数将感兴趣的分支进行放大展示，但是这个时候需要我们知道感兴趣的进化分支所对应的<code>node</code>编号才行。</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">clade</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset</a></span><span class="op">(</span><span class="va">beast_tree</span>, node<span class="op">=</span><span class="fl">121</span>, levels_back<span class="op">=</span><span class="fl">0</span><span class="op">)</span>
<span class="va">clade2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset</a></span><span class="op">(</span><span class="va">beast_tree</span>, node<span class="op">=</span><span class="fl">121</span>, levels_back<span class="op">=</span><span class="fl">2</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">clade</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'感兴趣的整个分支'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">5</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">clade2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'感兴趣的整个分支 + 回退两个节点'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">8</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span>


<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="http://ape-package.ird.fr/">ape</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">chiroptera</span><span class="op">)</span>

<span class="co"># 如果不知道node的时候，就直接进行匹配</span>NA<span class="va">nodes</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/grep.html">grep</a></span><span class="op">(</span><span class="st">"Plecotus"</span>, <span class="va">chiroptera</span><span class="op">$</span><span class="va">tip.label</span><span class="op">)</span>
<span class="va">chiroptera</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">chiroptera</span>, <span class="va">nodes</span><span class="op">)</span>

<span class="va">p3</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">chiroptera</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>colour <span class="op">=</span> <span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'整个进化树中选择感兴趣的整个分支'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"red"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span>

<span class="va">clade</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/MRCA.html">MRCA</a></span><span class="op">(</span><span class="va">chiroptera</span>, <span class="va">nodes</span><span class="op">)</span> <span class="co"># 最近的父节点</span>NA<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/tree_subset.html">tree_subset</a></span><span class="op">(</span><span class="va">chiroptera</span>, <span class="va">clade</span>, levels_back <span class="op">=</span> <span class="fl">0</span><span class="op">)</span>
<span class="va">p4</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'感兴趣的特有分支'</span>NA<span class="op">)</span>NA<span class="op">+</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">5</span><span class="op">)</span>

<span class="fu">plot_grid</span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>, <span class="va">p3</span>, <span class="va">p4</span>,ncol<span class="op">=</span><span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/10.png"></div>
</div>
</div>
<div id="导出数据" class="section level4" number="3.2.4.8">
<h4>
<span class="header-section-number">3.2.4.8</span> 导出数据<a class="anchor" aria-label="anchor" href="#%E5%AF%BC%E5%87%BA%E6%95%B0%E6%8D%AE"><i class="fas fa-link"></i></a>
</h4>
<p><code>treeio</code>这个R包可以导出多种格式的文件，这个比较简单，详情参照：<a href="https://yulab-smu.top/treedata-book/chapter3.html" class="uri">https://yulab-smu.top/treedata-book/chapter3.html</a></p>
</div>
</div>
<div id="进化树可视化" class="section level3" number="3.2.5">
<h3>
<span class="header-section-number">3.2.5</span> 进化树可视化<a class="anchor" aria-label="anchor" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E5%8F%AF%E8%A7%86%E5%8C%96"><i class="fas fa-link"></i></a>
</h3>
<div id="基础方法" class="section level4" number="3.2.5.1">
<h4>
<span class="header-section-number">3.2.5.1</span> 基础方法<a class="anchor" aria-label="anchor" href="#%E5%9F%BA%E7%A1%80%E6%96%B9%E6%B3%95"><i class="fas fa-link"></i></a>
</h4>
<p><code>ggtree</code>对进化树进行可视化的方法有两种：</p>
<ul>
<li>
<code><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot()</a></code> + <code><a href="https://rdrr.io/pkg/ggtree/man/geom_tree.html">geom_tree()</a></code> + <code><a href="https://rdrr.io/pkg/ggtree/man/theme_tree.html">theme_tree()</a></code>
</li>
<li><code><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree()</a></code></li>
</ul>
<p>第二种方法是第一种方法的“缩写版”。</p>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>

<span class="va">nwk</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"sample.nwk"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">nwk</span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">tree</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tree.html">geom_tree</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree.html">theme_tree</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span></code></pre></div>
<p>这两种方法得到的结果是一样的：</p>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/11.png"></div>
<p><code>ggtree</code>支持<code>ggplot2</code>的图形语法，因此，也可以在<code>ggtree</code>中对颜色性状等进行修改：</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="r-vis.html#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb42-2"><a href="r-vis.html#cb42-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(treeio)</span>
<span id="cb42-3"><a href="r-vis.html#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggtree)</span>
<span id="cb42-4"><a href="r-vis.html#cb42-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-5"><a href="r-vis.html#cb42-5" aria-hidden="true" tabindex="-1"></a>nwk <span class="ot">&lt;-</span> <span class="fu">system.file</span>(<span class="st">"extdata"</span>, <span class="st">"sample.nwk"</span>, <span class="at">package=</span><span class="st">"treeio"</span>)</span>
<span id="cb42-6"><a href="r-vis.html#cb42-6" aria-hidden="true" tabindex="-1"></a>tree <span class="ot">&lt;-</span> <span class="fu">read.tree</span>(nwk)</span>
<span id="cb42-7"><a href="r-vis.html#cb42-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-8"><a href="r-vis.html#cb42-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(tree, <span class="fu">aes</span>(x, y)) <span class="sc">+</span> <span class="fu">geom_tree</span>() <span class="sc">+</span> <span class="fu">theme_tree</span>()</span>
<span id="cb42-9"><a href="r-vis.html#cb42-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggtree</span>(tree)</span>
<span id="cb42-10"><a href="r-vis.html#cb42-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-11"><a href="r-vis.html#cb42-11" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">ggtree</span>(tree, <span class="at">color=</span><span class="st">"firebrick"</span>, <span class="at">size=</span><span class="dv">2</span>, <span class="at">linetype=</span><span class="st">"dotted"</span>) <span class="sc">+</span></span>
<span id="cb42-12"><a href="r-vis.html#cb42-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ggtitle</span>(<span class="st">'阶梯化'</span>)</span>
<span id="cb42-13"><a href="r-vis.html#cb42-13" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">=</span> <span class="fu">ggtree</span>(tree, <span class="at">color=</span><span class="st">"firebrick"</span>, <span class="at">size=</span><span class="dv">2</span>, <span class="at">linetype=</span><span class="st">"dotted"</span>, <span class="at">ladderize=</span><span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb42-14"><a href="r-vis.html#cb42-14" aria-hidden="true" tabindex="-1"></a>  <span class="fu">ggtitle</span>(<span class="st">'非阶梯化'</span>)</span>
<span id="cb42-15"><a href="r-vis.html#cb42-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-16"><a href="r-vis.html#cb42-16" aria-hidden="true" tabindex="-1"></a>cowplot<span class="sc">::</span><span class="fu">plot_grid</span>(p1,p2)v</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/12.png"></div>
<p>可以使用参数<code>branch.length</code>对egde进行标准化，如果参数为<code>none</code>，则是这样：</p>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, branch.length<span class="op">=</span><span class="st">"none"</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/13.png"></div>
</div>
<div id="输出样式" class="section level4" number="3.2.5.2">
<h4>
<span class="header-section-number">3.2.5.2</span> 输出样式<a class="anchor" aria-label="anchor" href="#%E8%BE%93%E5%87%BA%E6%A0%B7%E5%BC%8F"><i class="fas fa-link"></i></a>
</h4>
<p><code>ggtree</code>支持多种输出样式：</p>
<div class="sourceCode" id="cb44"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2017</span><span class="op">-</span><span class="fl">02</span><span class="op">-</span><span class="fl">16</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">50</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'默认'</span>NA<span class="op">)</span>NA<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"roundrect"</span><span class="op">)</span>  <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'roundrect'</span><span class="op">)</span>
<span class="va">p3</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"slanted"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'slanted'</span><span class="op">)</span>
<span class="va">p4</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"ellipse"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'ellipse'</span><span class="op">)</span>
<span class="va">p5</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"circular"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'circular'</span><span class="op">)</span>
<span class="va">p6</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"fan"</span>, open.angle<span class="op">=</span><span class="fl">120</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'fan'</span><span class="op">)</span>
<span class="va">p7</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"equal_angle"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'equal_angle'</span><span class="op">)</span>
<span class="va">p8</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"daylight"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'daylight'</span><span class="op">)</span>
<span class="va">p9</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, branch.length<span class="op">=</span><span class="st">'none'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'none'</span><span class="op">)</span>
<span class="va">p10</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"ellipse"</span>, branch.length<span class="op">=</span><span class="st">"none"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'ellipse对齐'</span>NA<span class="op">)</span>NA<span class="va">p11</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, branch.length<span class="op">=</span><span class="st">'none'</span>, layout<span class="op">=</span><span class="st">'circular'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'circular对齐'</span>NA<span class="op">)</span>NA<span class="va">p12</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"daylight"</span>, branch.length <span class="op">=</span> <span class="st">'none'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'daylight对齐'</span>NA<span class="op">)</span>NA<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,<span class="va">p3</span>,<span class="va">p4</span>,<span class="va">p5</span>,<span class="va">p6</span>,<span class="va">p7</span>,<span class="va">p8</span>,<span class="va">p9</span>,<span class="va">p10</span>,<span class="va">p11</span>,<span class="va">p12</span>, ncol <span class="op">=</span> <span class="fl">4</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/14.png"></div>
<p>当只是展示树结构而没有分支长度标尺的时候，就用最下面这4种。</p>
<p>还有其他的多种对齐方式：</p>
<div class="sourceCode" id="cb45"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_continuous.html">scale_x_reverse</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/coord_flip.html">coord_flip</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_dendrogram</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu">ggplotify</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggplotify/man/as.ggplot.html">as.ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>, angle<span class="op">=</span><span class="op">-</span><span class="fl">30</span>, scale<span class="op">=</span><span class="fl">.9</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">'slanted'</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/coord_flip.html">coord_flip</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">'slanted'</span>, branch.length<span class="op">=</span><span class="st">'none'</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_dendrogram</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">'circular'</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="op">-</span><span class="fl">10</span>, <span class="cn">NA</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_inward_circular</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_inward_circular</a></span><span class="op">(</span>xlim<span class="op">=</span><span class="fl">15</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/15.png"></div>
<p>如果是时间范围数据的话，则需要调用参数<code>mrsd</code>（most recent sample data）：</p>
<div class="sourceCode" id="cb46"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">beast_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/MCC_FluA_H3.tree"</span>, 
                          package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">beast_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.beast</a></span><span class="op">(</span><span class="va">beast_file</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree</span>, mrsd<span class="op">=</span><span class="st">"2013-01-01"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/16.png"></div>
</div>
<div id="展示相关元素" class="section level4" number="3.2.5.3">
<h4>
<span class="header-section-number">3.2.5.3</span> 展示相关元素<a class="anchor" aria-label="anchor" href="#%E5%B1%95%E7%A4%BA%E7%9B%B8%E5%85%B3%E5%85%83%E7%B4%A0"><i class="fas fa-link"></i></a>
</h4>
<div id="展示进化距离" class="section level5" number="3.2.5.3.1">
<h5>
<span class="header-section-number">3.2.5.3.1</span> 展示进化距离<a class="anchor" aria-label="anchor" href="#%E5%B1%95%E7%A4%BA%E8%BF%9B%E5%8C%96%E8%B7%9D%E7%A6%BB"><i class="fas fa-link"></i></a>
</h5>
<p>函数<code><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale()</a></code>用于展示进化距离。</p>
<div class="sourceCode" id="cb47"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'默认'</span>NA<span class="op">)</span>NA<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale</a></span><span class="op">(</span>x<span class="op">=</span><span class="fl">0</span>, y<span class="op">=</span><span class="fl">45</span>, width<span class="op">=</span><span class="fl">1</span>, color<span class="op">=</span><span class="st">'red'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'设定位置、宽度、颜色'</span>NA<span class="op">)</span>NA<span class="va">p3</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale</a></span><span class="op">(</span>fontsize<span class="op">=</span><span class="fl">6</span>, linesize<span class="op">=</span><span class="fl">2</span>, offset<span class="op">=</span><span class="fl">1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'设定字体大小、线条大小、缩进'</span>NA<span class="op">)</span>NA<span class="va">p4</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'使用内置主题'</span>NA<span class="op">)</span>NA<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,<span class="va">p3</span>,<span class="va">p4</span>, ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/17.png"></div>
</div>
<div id="展示nodestips" class="section level5" number="3.2.5.3.2">
<h5>
<span class="header-section-number">3.2.5.3.2</span> 展示<code>nodes/tips</code><a class="anchor" aria-label="anchor" href="#%E5%B1%95%E7%A4%BAnodestips"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb48"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_point.html">geom_point</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>shape<span class="op">=</span><span class="va">isTip</span>, color<span class="op">=</span><span class="va">isTip</span><span class="op">)</span>, size<span class="op">=</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'使用geom_point()函数'</span>NA<span class="op">)</span>NA<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_nodepoint.html">geom_nodepoint</a></span><span class="op">(</span>color<span class="op">=</span><span class="st">"#b5e521"</span>, alpha<span class="op">=</span><span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, size<span class="op">=</span><span class="fl">10</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span>color<span class="op">=</span><span class="st">"#FDAC4F"</span>, shape<span class="op">=</span><span class="fl">8</span>, size<span class="op">=</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'使用两个函数'</span>NA<span class="op">)</span>NA<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>, ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/18.png"></div>
</div>
<div id="展示tips的标签" class="section level5" number="3.2.5.3.3">
<h5>
<span class="header-section-number">3.2.5.3.3</span> 展示<code>tips</code>的标签<a class="anchor" aria-label="anchor" href="#%E5%B1%95%E7%A4%BAtips%E7%9A%84%E6%A0%87%E7%AD%BE"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb49"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_nodepoint.html">geom_nodepoint</a></span><span class="op">(</span>color<span class="op">=</span><span class="st">"#b5e521"</span>, alpha<span class="op">=</span><span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, size<span class="op">=</span><span class="fl">10</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span>color<span class="op">=</span><span class="st">"#FDAC4F"</span>, shape<span class="op">=</span><span class="fl">8</span>, size<span class="op">=</span><span class="fl">3</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">3</span>, color<span class="op">=</span><span class="st">"purple"</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"circular"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>angle<span class="op">=</span><span class="va">angle</span><span class="op">)</span>, color<span class="op">=</span><span class="st">'blue'</span><span class="op">)</span>

<span class="va">p3</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, branch.length <span class="op">=</span> <span class="st">'none'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>as_ylab<span class="op">=</span><span class="cn">TRUE</span>, color<span class="op">=</span><span class="st">'firebrick'</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>,<span class="va">p3</span>, ncol <span class="op">=</span> <span class="fl">3</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/19.png"></div>
</div>
<div id="展示根节点" class="section level5" number="3.2.5.3.4">
<h5>
<span class="header-section-number">3.2.5.3.4</span> 展示根节点<a class="anchor" aria-label="anchor" href="#%E5%B1%95%E7%A4%BA%E6%A0%B9%E8%8A%82%E7%82%B9"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb50"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## with root edge = 1</span>
<span class="va">tree1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text<span class="op">=</span><span class="st">'((A:1,B:2):3,C:2):1;'</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree1</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_rootedge.html">geom_rootedge</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'有根节点信息'</span>NA<span class="op">)</span>NA<span class="co">## without root edge</span>
<span class="va">tree2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text<span class="op">=</span><span class="st">'((A:1,B:2):3,C:2);'</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_rootedge.html">geom_rootedge</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'无根节点信息，默认无'</span>NA<span class="op">)</span>NA<span class="co">## setting root edge</span>
<span class="va">tree2</span><span class="op">$</span><span class="va">root.edge</span> <span class="op">&lt;-</span> <span class="fl">2</span>
<span class="va">p3</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_rootedge.html">geom_rootedge</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'无根节点信息，添加信息'</span>NA<span class="op">)</span>NA<span class="co">## specify length of root edge for just plotting</span>
<span class="co">## this will ignore tree$root.edge</span>
<span class="va">p4</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_rootedge.html">geom_rootedge</a></span><span class="op">(</span>rootedge <span class="op">=</span> <span class="fl">3</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/labs.html">ggtitle</a></span><span class="op">(</span><span class="st">'无根节点信息，设置信息'</span>NA<span class="op">)</span>NA<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,<span class="va">p3</span>,<span class="va">p4</span>, ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/20.png"></div>
</div>
<div id="上色" class="section level5" number="3.2.5.3.5">
<h5>
<span class="header-section-number">3.2.5.3.5</span> 上色<a class="anchor" aria-label="anchor" href="#%E4%B8%8A%E8%89%B2"><i class="fas fa-link"></i></a>
</h5>
<p>上色直接是很简单，就像<code>ggplot2</code>那样：</p>
<div class="sourceCode" id="cb51"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">rate</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_colour_continuous.html">scale_color_continuous</a></span><span class="op">(</span>low<span class="op">=</span><span class="st">'darkgreen'</span>, high<span class="op">=</span><span class="st">'red'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">"right"</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/21.png"></div>
<div class="sourceCode" id="cb52"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">anole.tree</span><span class="op">&lt;-</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"http://www.phytools.org/eqg2015/data/anole.tre"</span><span class="op">)</span>
<span class="va">svl</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"http://www.phytools.org/eqg2015/data/svl.csv"</span>,
                row.names<span class="op">=</span><span class="fl">1</span><span class="op">)</span>
<span class="va">svl</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/matrix.html">as.matrix</a></span><span class="op">(</span><span class="va">svl</span><span class="op">)</span><span class="op">[</span>,<span class="fl">1</span><span class="op">]</span>
<span class="va">fit</span> <span class="op">&lt;-</span> <span class="fu">phytools</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/phytools/man/fastAnc.html">fastAnc</a></span><span class="op">(</span><span class="va">anole.tree</span>,<span class="va">svl</span>,vars<span class="op">=</span><span class="cn">TRUE</span>,CI<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span>

<span class="va">td</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/nodeid.html">nodeid</a></span><span class="op">(</span><span class="va">anole.tree</span>, <span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">svl</span><span class="op">)</span><span class="op">)</span>,
                 trait <span class="op">=</span> <span class="va">svl</span><span class="op">)</span>
<span class="va">nd</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">fit</span><span class="op">$</span><span class="va">ace</span><span class="op">)</span>, trait <span class="op">=</span> <span class="va">fit</span><span class="op">$</span><span class="va">ace</span><span class="op">)</span>

<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/cbind.html">rbind</a></span><span class="op">(</span><span class="va">td</span>, <span class="va">nd</span><span class="op">)</span>
<span class="va">d</span><span class="op">$</span><span class="va">node</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="va">d</span><span class="op">$</span><span class="va">node</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">anole.tree</span>, <span class="va">d</span>, by <span class="op">=</span> <span class="st">'node'</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">trait</span><span class="op">)</span>, layout <span class="op">=</span> <span class="st">'circular'</span>, 
             ladderize <span class="op">=</span> <span class="cn">FALSE</span>, continuous <span class="op">=</span> <span class="cn">TRUE</span>, size<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_gradient.html">scale_color_gradientn</a></span><span class="op">(</span>colours<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"red"</span>, <span class="st">'orange'</span>, <span class="st">'green'</span>, <span class="st">'cyan'</span>, <span class="st">'blue'</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>hjust <span class="op">=</span> <span class="op">-</span><span class="fl">.1</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1.2</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.05</span>, <span class="fl">.85</span><span class="op">)</span><span class="op">)</span> 

<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">'circular'</span>, ladderize <span class="op">=</span> <span class="cn">FALSE</span>, size<span class="op">=</span><span class="fl">2.8</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tree.html">geom_tree</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">trait</span><span class="op">)</span>, continuous<span class="op">=</span><span class="cn">T</span>, size<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">+</span>  
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_gradient.html">scale_color_gradientn</a></span><span class="op">(</span>colours<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"red"</span>, <span class="st">'orange'</span>, <span class="st">'green'</span>, <span class="st">'cyan'</span>, <span class="st">'blue'</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">trait</span><span class="op">)</span>, hjust <span class="op">=</span> <span class="op">-</span><span class="fl">.1</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1.2</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.05</span>, <span class="fl">.85</span><span class="op">)</span><span class="op">)</span> 

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>, ncol<span class="op">=</span><span class="fl">2</span>, labels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"分支默认边框"</span>色边<span class="st">"分支黑色边框"</span>NA<span class="op">)</span>NA<span class="op">)</span>NA</code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/22.png"></div>
</div>
<div id="对树进行重新标准化" class="section level5" number="3.2.5.3.6">
<h5>
<span class="header-section-number">3.2.5.3.6</span> 对树进行重新标准化<a class="anchor" aria-label="anchor" href="#%E5%AF%B9%E6%A0%91%E8%BF%9B%E8%A1%8C%E9%87%8D%E6%96%B0%E6%A0%87%E5%87%86%E5%8C%96"><i class="fas fa-link"></i></a>
</h5>
<p>多个参数对进化树进行标准化，时间序列相关的参数用<code>msrd</code>，其余的参数可以用<code>branch.length</code>。</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="r-vis.html#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(treeio)</span>
<span id="cb53-2"><a href="r-vis.html#cb53-2" aria-hidden="true" tabindex="-1"></a>beast_file <span class="ot">&lt;-</span> <span class="fu">system.file</span>(<span class="st">"examples/MCC_FluA_H3.tree"</span>, <span class="at">package=</span><span class="st">"ggtree"</span>)</span>
<span id="cb53-3"><a href="r-vis.html#cb53-3" aria-hidden="true" tabindex="-1"></a>beast_tree <span class="ot">&lt;-</span> <span class="fu">read.beast</span>(beast_file)</span>
<span id="cb53-4"><a href="r-vis.html#cb53-4" aria-hidden="true" tabindex="-1"></a>beast_tree</span>
<span id="cb53-5"><a href="r-vis.html#cb53-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-6"><a href="r-vis.html#cb53-6" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">&lt;-</span> <span class="fu">ggtree</span>(beast_tree, <span class="at">mrsd=</span><span class="st">'2013-01-01'</span>) <span class="sc">+</span> <span class="fu">theme_tree2</span>() <span class="sc">+</span></span>
<span id="cb53-7"><a href="r-vis.html#cb53-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">caption=</span><span class="st">"时间序列"</span>)</span>
<span id="cb53-8"><a href="r-vis.html#cb53-8" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">&lt;-</span> <span class="fu">ggtree</span>(beast_tree, <span class="at">branch.length=</span><span class="st">'rate'</span>) <span class="sc">+</span> <span class="fu">theme_tree2</span>() <span class="sc">+</span></span>
<span id="cb53-9"><a href="r-vis.html#cb53-9" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">caption=</span><span class="st">"取代速率"</span>)</span>
<span id="cb53-10"><a href="r-vis.html#cb53-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-11"><a href="r-vis.html#cb53-11" aria-hidden="true" tabindex="-1"></a>mlcfile <span class="ot">&lt;-</span> <span class="fu">system.file</span>(<span class="st">"extdata/PAML_Codeml"</span>, <span class="st">"mlc"</span>, <span class="at">package=</span><span class="st">"treeio"</span>)</span>
<span id="cb53-12"><a href="r-vis.html#cb53-12" aria-hidden="true" tabindex="-1"></a>mlc_tree <span class="ot">&lt;-</span> <span class="fu">read.codeml_mlc</span>(mlcfile)</span>
<span id="cb53-13"><a href="r-vis.html#cb53-13" aria-hidden="true" tabindex="-1"></a>p3 <span class="ot">&lt;-</span> <span class="fu">ggtree</span>(mlc_tree) <span class="sc">+</span> <span class="fu">theme_tree2</span>() <span class="sc">+</span></span>
<span id="cb53-14"><a href="r-vis.html#cb53-14" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">caption=</span><span class="st">"单密码子核苷酸取代"</span>)</span>
<span id="cb53-15"><a href="r-vis.html#cb53-15" aria-hidden="true" tabindex="-1"></a>p4 <span class="ot">&lt;-</span> <span class="fu">ggtree</span>(mlc_tree, <span class="at">branch.length=</span><span class="st">'dN_vs_dS'</span>) <span class="sc">+</span> <span class="fu">theme_tree2</span>() <span class="sc">+</span></span>
<span id="cb53-16"><a href="r-vis.html#cb53-16" aria-hidden="true" tabindex="-1"></a>  <span class="fu">labs</span>(<span class="at">caption=</span><span class="st">"dN/dS"</span>)</span>
<span id="cb53-17"><a href="r-vis.html#cb53-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-18"><a href="r-vis.html#cb53-18" aria-hidden="true" tabindex="-1"></a>cowplot<span class="sc">::</span><span class="fu">plot_grid</span>(p1,p2,p3,p4, <span class="at">ncol =</span> <span class="dv">2</span></span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/23.png"></div>
<p>也可以用<code>rescale_tree</code>对树进行标准化：</p>
<div class="sourceCode" id="cb54"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">beast_tree2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/rescale_tree.html">rescale_tree</a></span><span class="op">(</span><span class="va">beast_tree</span>, branch.length<span class="op">=</span><span class="st">'rate'</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/24.png"></div>
</div>
<div id="背景色" class="section level5" number="3.2.5.3.7">
<h5>
<span class="header-section-number">3.2.5.3.7</span> 背景色<a class="anchor" aria-label="anchor" href="#%E8%83%8C%E6%99%AF%E8%89%B2"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb55"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2019</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">30</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span>, color<span class="op">=</span><span class="st">"red"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree.html">theme_tree</a></span><span class="op">(</span><span class="st">"steelblue"</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span>, color<span class="op">=</span><span class="st">"white"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree.html">theme_tree</a></span><span class="op">(</span><span class="st">"black"</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>, ncol <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/25.png"></div>
</div>
<div id="批量建树" class="section level5" number="3.2.5.3.8">
<h5>
<span class="header-section-number">3.2.5.3.8</span> 批量建树<a class="anchor" aria-label="anchor" href="#%E6%89%B9%E9%87%8F%E5%BB%BA%E6%A0%91"><i class="fas fa-link"></i></a>
</h5>
<p>批量建树就相当于<code>ggplot2</code>中的分面：</p>
<div class="sourceCode" id="cb56"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">## trees &lt;- lapply(c(10, 20, 40), rtree)</span>
<span class="co">## class(trees) &lt;- "multiPhylo"</span>
<span class="co">## ggtree(trees) + facet_wrap(~.id, scale="free") + geom_tiplab()</span>

<span class="va">r8s</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.r8s.html">read.r8s</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/r8s"</span>, <span class="st">"H3_r8s_output.log"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">r8s</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/facet_wrap.html">facet_wrap</a></span><span class="op">(</span> <span class="op">~</span><span class="va">.id</span>, scale<span class="op">=</span><span class="st">"free"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/26.png"></div>
</div>
</div>
</div>
<div id="进化树注释" class="section level3" number="3.2.6">
<h3>
<span class="header-section-number">3.2.6</span> 进化树注释<a class="anchor" aria-label="anchor" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h3>
<div id="基本用法" class="section level4" number="3.2.6.1">
<h4>
<span class="header-section-number">3.2.6.1</span> 基本用法<a class="anchor" aria-label="anchor" href="#%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><i class="fas fa-link"></i></a>
</h4>
<p>注释的信息很多，节点的分组、样品来源、基因表达量等都可以作为注释的信息。一个简单的例子：</p>
<div class="sourceCode" id="cb57"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="va">treetext</span> <span class="op">=</span> <span class="st">"(((ADH2:0.1[&amp;&amp;NHX:S=human], ADH1:0.11[&amp;&amp;NHX:S=human]):
0.05 [&amp;&amp;NHX:S=primates:D=Y:B=100],ADHY:
0.1[&amp;&amp;NHX:S=nematode],ADHX:0.12 [&amp;&amp;NHX:S=insect]):
0.1[&amp;&amp;NHX:S=metazoa:D=N],(ADH4:0.09[&amp;&amp;NHX:S=yeast],
ADH3:0.13[&amp;&amp;NHX:S=yeast], ADH2:0.12[&amp;&amp;NHX:S=yeast],
ADH1:0.11[&amp;&amp;NHX:S=yeast]):0.1[&amp;&amp;NHX:S=Fungi])[&amp;&amp;NHX:D=N];"</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.nhx.html">read.nhx</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/textconnections.html">textConnection</a></span><span class="op">(</span><span class="va">treetext</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_label</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">branch</span>, label<span class="op">=</span><span class="va">S</span><span class="op">)</span>, fill<span class="op">=</span><span class="st">'lightgreen'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_label</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">D</span><span class="op">)</span>, fill<span class="op">=</span><span class="st">'steelblue'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_text</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">B</span><span class="op">)</span>, hjust<span class="op">=</span><span class="op">-</span><span class="fl">.5</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/27.png"></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/28.png"></div>
</div>
<div id="clade注释" class="section level4" number="3.2.6.2">
<h4>
<span class="header-section-number">3.2.6.2</span> <code>clade</code>注释<a class="anchor" aria-label="anchor" href="#clade%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h4>
<div class="sourceCode" id="cb58"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2015</span><span class="op">-</span><span class="fl">12</span><span class="op">-</span><span class="fl">21</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">30</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">6</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">=</span> <span class="va">p1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">45</span>, label<span class="op">=</span><span class="st">"test label"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">34</span>, label<span class="op">=</span><span class="st">"another clade"</span><span class="op">)</span>

<span class="va">p3</span> <span class="op">=</span> <span class="va">p1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">45</span>, label<span class="op">=</span><span class="st">"test label"</span>, align<span class="op">=</span><span class="cn">TRUE</span>,  offset <span class="op">=</span> <span class="fl">.2</span>, color<span class="op">=</span><span class="st">'red'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">34</span>, label<span class="op">=</span><span class="st">"another clade"</span>, align<span class="op">=</span><span class="cn">TRUE</span>, offset <span class="op">=</span> <span class="fl">.2</span>, color<span class="op">=</span><span class="st">'blue'</span><span class="op">)</span>

<span class="va">p4</span> <span class="op">=</span> <span class="va">p1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">45</span>, label<span class="op">=</span><span class="st">"test label"</span>, align<span class="op">=</span><span class="cn">T</span>, angle<span class="op">=</span><span class="fl">270</span>, hjust<span class="op">=</span><span class="st">'center'</span>, offset.text<span class="op">=</span><span class="fl">.5</span>, barsize<span class="op">=</span><span class="fl">1.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">34</span>, label<span class="op">=</span><span class="st">"another clade"</span>, align<span class="op">=</span><span class="cn">T</span>, angle<span class="op">=</span><span class="fl">45</span>, fontsize<span class="op">=</span><span class="fl">8</span><span class="op">)</span>

<span class="va">p5</span> <span class="op">=</span> <span class="va">p1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">34</span>, label<span class="op">=</span><span class="st">"another clade"</span>, align<span class="op">=</span><span class="cn">T</span>, geom<span class="op">=</span><span class="st">'label'</span>, fill<span class="op">=</span><span class="st">'lightblue'</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p2</span>,<span class="va">p3</span>,<span class="va">p4</span>,<span class="va">p5</span>,ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/29.png"></div>
<p>同样也适用于无根输出样式，可以根据<code>node</code>的编号，也可以直接用<code>tips</code>的标签（这个是真的赞啊）：</p>
<div class="sourceCode" id="cb59"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"daylight"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">35</span>, label<span class="op">=</span><span class="st">"test label"</span>, angle<span class="op">=</span><span class="fl">0</span>, 
                  fontsize<span class="op">=</span><span class="fl">8</span>, offset<span class="op">=</span><span class="fl">.5</span>, vjust<span class="op">=</span><span class="fl">.5</span><span class="op">)</span>  <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">55</span>, label<span class="op">=</span><span class="st">'another clade'</span>, 
                  angle<span class="op">=</span><span class="op">-</span><span class="fl">95</span>, hjust<span class="op">=</span><span class="fl">.5</span>, fontsize<span class="op">=</span><span class="fl">8</span><span class="op">)</span>


<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">6</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_strip.html">geom_strip</a></span><span class="op">(</span><span class="st">'t10'</span>, <span class="st">'t30'</span>, barsize<span class="op">=</span><span class="fl">2</span>, color<span class="op">=</span><span class="st">'red'</span>, 
             label<span class="op">=</span><span class="st">"associated taxa"</span>, offset.text<span class="op">=</span><span class="fl">.1</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_strip.html">geom_strip</a></span><span class="op">(</span><span class="st">'t1'</span>, <span class="st">'t18'</span>, barsize<span class="op">=</span><span class="fl">2</span>, color<span class="op">=</span><span class="st">'blue'</span>, 
             label <span class="op">=</span> <span class="st">"another label"</span>, offset.text<span class="op">=</span><span class="fl">.1</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/30.png"></div>
<div id="局部高亮" class="section level5" number="3.2.6.2.1">
<h5>
<span class="header-section-number">3.2.6.2.1</span> 局部高亮<a class="anchor" aria-label="anchor" href="#%E5%B1%80%E9%83%A8%E9%AB%98%E4%BA%AE"><i class="fas fa-link"></i></a>
</h5>
<p>局部高亮可以根据<code>node</code>编号进行高亮，也可以使用附加数据或树数据里面的数据进行高亮：</p>
<div class="sourceCode" id="cb60"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">nwk</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"sample.nwk"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">nwk</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">21</span>, fill<span class="op">=</span><span class="st">"steelblue"</span>, alpha<span class="op">=</span><span class="fl">.6</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">17</span>, fill<span class="op">=</span><span class="st">"darkgreen"</span>, alpha<span class="op">=</span><span class="fl">.6</span><span class="op">)</span> 

<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"circular"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">21</span>, fill<span class="op">=</span><span class="st">"steelblue"</span>, alpha<span class="op">=</span><span class="fl">.6</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">23</span>, fill<span class="op">=</span><span class="st">"darkgreen"</span>, alpha<span class="op">=</span><span class="fl">.6</span><span class="op">)</span>

<span class="co">## type can be 'encircle' or 'rect'</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"daylight"</span>, branch.length <span class="op">=</span> <span class="st">'none'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">10</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">16</span>, fill<span class="op">=</span><span class="st">'darkgreen'</span>, type<span class="op">=</span><span class="st">"rect"</span><span class="op">)</span>


<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_balance.html">geom_balance</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">16</span>, fill<span class="op">=</span><span class="st">'steelblue'</span>, color<span class="op">=</span><span class="st">'white'</span>, alpha<span class="op">=</span><span class="fl">0.6</span>, extend<span class="op">=</span><span class="fl">1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_balance.html">geom_balance</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">19</span>, fill<span class="op">=</span><span class="st">'darkgreen'</span>, color<span class="op">=</span><span class="st">'white'</span>, alpha<span class="op">=</span><span class="fl">0.6</span>, extend<span class="op">=</span><span class="fl">1</span><span class="op">)</span> 

<span class="co">## using external data</span>
<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>node<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">17</span>, <span class="fl">21</span><span class="op">)</span>, type<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">d</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>node<span class="op">=</span><span class="va">node</span>, fill<span class="op">=</span><span class="va">type</span><span class="op">)</span><span class="op">)</span>

<span class="co">## using data stored in tree object</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.nhx.html">read.nhx</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/NHX/ADH.nhx"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>subset <span class="op">=</span> <span class="va">node</span> <span class="op">%in%</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">10</span>, <span class="fl">12</span><span class="op">)</span>, fill <span class="op">=</span> <span class="va">S</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"steelblue"</span>, <span class="st">"darkgreen"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/31.png"></div>
</div>
<div id="高亮不同分组" class="section level5" number="3.2.6.2.2">
<h5>
<span class="header-section-number">3.2.6.2.2</span> 高亮不同分组<a class="anchor" aria-label="anchor" href="#%E9%AB%98%E4%BA%AE%E4%B8%8D%E5%90%8C%E5%88%86%E7%BB%84"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb61"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">mytree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"data/Tree 30.4.19.nwk"</span><span class="op">)</span>

<span class="co"># Define nodes for coloring later on</span>
<span class="va">tiplab</span> <span class="op">&lt;-</span> <span class="va">mytree</span><span class="op">$</span><span class="va">tip.label</span>
<span class="va">cls</span> <span class="op">&lt;-</span> <span class="va">tiplab</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/grep.html">grep</a></span><span class="op">(</span><span class="st">"^ch"</span>, <span class="va">tiplab</span><span class="op">)</span><span class="op">]</span> 
<span class="va">labeltree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">mytree</span>, <span class="va">cls</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">labeltree</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span>, linetype<span class="op">=</span><span class="va">group</span><span class="op">)</span>, layout<span class="op">=</span><span class="st">"circular"</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"#efad29"</span>, <span class="st">"#63bbd4"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_nodepoint.html">geom_nodepoint</a></span><span class="op">(</span>color<span class="op">=</span><span class="st">"black"</span>, size<span class="op">=</span><span class="fl">0.1</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">2</span>, color<span class="op">=</span><span class="st">"black"</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/flip.html">flip</a></span><span class="op">(</span><span class="va">p</span>, <span class="fl">136</span>, <span class="fl">110</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/flip.html">flip</a></span><span class="op">(</span><span class="fl">141</span>, <span class="fl">145</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">141</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">142</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">160</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">164</span><span class="op">)</span> <span class="op">%&gt;%</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">131</span><span class="op">)</span>

<span class="co">### Group V and II coloring </span>
<span class="va">p3</span> <span class="op">&lt;-</span> <span class="va">p2</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">110</span>, fill <span class="op">=</span> <span class="st">"#229f8a"</span>, alpha <span class="op">=</span> <span class="fl">0.2</span>, extend <span class="op">=</span> <span class="fl">0.43</span><span class="op">)</span> <span class="op">+</span>    
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">88</span>, fill <span class="op">=</span> <span class="st">"#229f8a"</span>, alpha <span class="op">=</span> <span class="fl">0.2</span>, extend <span class="op">=</span> <span class="fl">0.445</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">156</span>, fill <span class="op">=</span> <span class="st">"#229f8a"</span>, alpha <span class="op">=</span> <span class="fl">0.2</span>, extend <span class="op">=</span> <span class="fl">0.35</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">136</span>, fill <span class="op">=</span> <span class="st">"#f9311f"</span>, alpha <span class="op">=</span> <span class="fl">0.2</span>, extend <span class="op">=</span> <span class="fl">0.512</span><span class="op">)</span>

<span class="co">### Putting on a label on the avian specific expansion </span>
<span class="va">p4</span> <span class="op">&lt;-</span> <span class="va">p3</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">113</span>, label <span class="op">=</span> <span class="st">"Avian-specific expansion"</span>, 
                        align <span class="op">=</span> <span class="cn">TRUE</span>, angle <span class="op">=</span> <span class="op">-</span><span class="fl">35</span>, offset.text <span class="op">=</span> <span class="fl">0.05</span>, 
                        hjust <span class="op">=</span> <span class="st">"center"</span>, fontsize <span class="op">=</span> <span class="fl">2</span>,  offset <span class="op">=</span> <span class="fl">0.2</span>, barsize <span class="op">=</span> <span class="fl">0.2</span><span class="op">)</span>
    
<span class="co">### Adding the bootstrap values with subset used to remove all bootstraps &lt; 50  </span>
<span class="va">p5</span> <span class="op">&lt;-</span> <span class="va">p4</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_text2.html">geom_text2</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">label</span>, 
                        subset <span class="op">=</span> <span class="op">!</span><span class="fu"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="va">label</span><span class="op">)</span><span class="op">)</span> <span class="op">&amp;</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="va">label</span><span class="op">)</span> <span class="op">&gt;</span> <span class="fl">50</span><span class="op">)</span>, 
                    size <span class="op">=</span> <span class="fl">2</span>, color <span class="op">=</span> <span class="st">"black"</span>,nudge_y <span class="op">=</span> <span class="fl">0.7</span>, nudge_x <span class="op">=</span> <span class="op">-</span><span class="fl">0.05</span><span class="op">)</span>
 
<span class="co">### Putting labels on the subgroups </span>
<span class="va">p6</span> <span class="op">&lt;-</span> <span class="va">p5</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">114</span>, label <span class="op">=</span> <span class="st">"Subgroup A"</span>, align <span class="op">=</span> <span class="cn">TRUE</span>, 
                        angle <span class="op">=</span> <span class="op">-</span><span class="fl">55</span>, offset.text <span class="op">=</span> <span class="fl">.03</span>, hjust <span class="op">=</span> <span class="st">"center"</span>, 
                        offset <span class="op">=</span> <span class="fl">0.05</span>, barsize <span class="op">=</span> <span class="fl">0.2</span>, fontsize <span class="op">=</span> <span class="fl">2</span><span class="op">)</span> <span class="op">+</span>
            <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node <span class="op">=</span> <span class="fl">121</span>, label <span class="op">=</span> <span class="st">"Subgroup B"</span>, align <span class="op">=</span> <span class="cn">TRUE</span>, 
                        angle <span class="op">=</span> <span class="op">-</span><span class="fl">15</span>, offset.text <span class="op">=</span> <span class="fl">.03</span>, hjust <span class="op">=</span> <span class="st">"center"</span>, 
                        offset <span class="op">=</span> <span class="fl">0.05</span>, barsize <span class="op">=</span> <span class="fl">0.2</span>, fontsize <span class="op">=</span> <span class="fl">2</span><span class="op">)</span> <span class="op">+</span>
            <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">"none"</span>, 
                plot.margin<span class="op">=</span><span class="fu">grid</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="op">-</span><span class="fl">15</span>,<span class="op">-</span><span class="fl">15</span>,<span class="op">-</span><span class="fl">15</span>,<span class="op">-</span><span class="fl">15</span><span class="op">)</span>, <span class="st">"mm"</span><span class="op">)</span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">p6</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/62.png"></div>
</div>
<div id="样品连接" class="section level5" number="3.2.6.2.3">
<h5>
<span class="header-section-number">3.2.6.2.3</span> 样品连接<a class="anchor" aria-label="anchor" href="#%E6%A0%B7%E5%93%81%E8%BF%9E%E6%8E%A5"><i class="fas fa-link"></i></a>
</h5>
<p>利用函数<code><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink()</a></code>可以实现样品之间的连线，但是只支持有限的几种形式：</p>
<div class="sourceCode" id="cb62"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'A'</span>, taxa2<span class="op">=</span><span class="st">'E'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'F'</span>, taxa2<span class="op">=</span><span class="st">'K'</span>, 
                color<span class="op">=</span><span class="st">'red'</span>, linetype <span class="op">=</span> <span class="st">'dashed'</span>,
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.02</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"circular"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'A'</span>, taxa2<span class="op">=</span><span class="st">'E'</span>, 
                color<span class="op">=</span><span class="st">"grey"</span>,alpha<span class="op">=</span><span class="fl">0.5</span>, 
                offset<span class="op">=</span><span class="fl">0.05</span>,arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'F'</span>, taxa2<span class="op">=</span><span class="st">'K'</span>, 
                color<span class="op">=</span><span class="st">'red'</span>, linetype <span class="op">=</span> <span class="st">'dashed'</span>, 
                alpha<span class="op">=</span><span class="fl">0.5</span>, offset<span class="op">=</span><span class="fl">0.05</span>,
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">"L"</span>, taxa2<span class="op">=</span><span class="st">"M"</span>, 
                color<span class="op">=</span><span class="st">"blue"</span>, alpha<span class="op">=</span><span class="fl">0.5</span>, 
                offset<span class="op">=</span><span class="fl">0.05</span>,hratio<span class="op">=</span><span class="fl">0.8</span>, 
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span>

<span class="co"># when the tree was created using reverse x, </span>
<span class="co"># we can set outward to FALSE, which will generate the inward curve lines.</span>
<span class="va">p3</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"inward_circular"</span>, xlim<span class="op">=</span><span class="fl">150</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'A'</span>, taxa2<span class="op">=</span><span class="st">'E'</span>, 
                color<span class="op">=</span><span class="st">"grey"</span>, alpha<span class="op">=</span><span class="fl">0.5</span>, 
                offset<span class="op">=</span><span class="op">-</span><span class="fl">0.2</span>, 
                outward<span class="op">=</span><span class="cn">FALSE</span>,
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">'F'</span>, taxa2<span class="op">=</span><span class="st">'K'</span>, 
                color<span class="op">=</span><span class="st">'red'</span>, linetype <span class="op">=</span> <span class="st">'dashed'</span>, 
                alpha<span class="op">=</span><span class="fl">0.5</span>, offset<span class="op">=</span><span class="op">-</span><span class="fl">0.2</span>,
                outward<span class="op">=</span><span class="cn">FALSE</span>,
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="st">"L"</span>, taxa2<span class="op">=</span><span class="st">"M"</span>, 
                color<span class="op">=</span><span class="st">"blue"</span>, alpha<span class="op">=</span><span class="fl">0.5</span>, 
                offset<span class="op">=</span><span class="op">-</span><span class="fl">0.2</span>, 
                outward<span class="op">=</span><span class="cn">FALSE</span>,
                arrow<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/arrow.html">arrow</a></span><span class="op">(</span>length<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.01</span>, <span class="st">"npc"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>hjust<span class="op">=</span><span class="fl">1</span><span class="op">)</span> 

<span class="va">dat</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>from<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"F"</span>, <span class="st">"L"</span><span class="op">)</span>, 
                  to<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"E"</span>, <span class="st">"K"</span>, <span class="st">"M"</span><span class="op">)</span>, 
                  h<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fl">1</span>, <span class="fl">0.1</span><span class="op">)</span>, 
                  type<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"t1"</span>, <span class="st">"t2"</span>, <span class="st">"t3"</span><span class="op">)</span>, 
                  s<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">2</span>, <span class="fl">1</span>, <span class="fl">2</span><span class="op">)</span><span class="op">)</span>
<span class="va">p4</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"inward_circular"</span>, xlim<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">150</span>, <span class="fl">0</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_taxalink.html">geom_taxalink</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">dat</span>, 
                mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>taxa1<span class="op">=</span><span class="va">from</span>, 
                            taxa2<span class="op">=</span><span class="va">to</span>, 
                            color<span class="op">=</span><span class="va">type</span>, 
                            size<span class="op">=</span><span class="va">s</span><span class="op">)</span>, 
                ncp<span class="op">=</span><span class="fl">10</span>,
                offset<span class="op">=</span><span class="fl">0.15</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>hjust<span class="op">=</span><span class="fl">1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_size.html">scale_size_continuous</a></span><span class="op">(</span>range<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">1</span>,<span class="fl">3</span><span class="op">)</span><span class="op">)</span>
<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>, <span class="va">p2</span>, <span class="va">p3</span>, <span class="va">p4</span>, ncol<span class="op">=</span><span class="fl">2</span>, labels<span class="op">=</span><span class="va">LETTERS</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">]</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/32.png"></div>
</div>
<div id="uncertainty-of-evolutionary-inference" class="section level5" number="3.2.6.2.4">
<h5>
<span class="header-section-number">3.2.6.2.4</span> Uncertainty of evolutionary inference<a class="anchor" aria-label="anchor" href="#uncertainty-of-evolutionary-inference"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb63"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/MEGA7"</span>, <span class="st">"mtCDNA_timetree.nex"</span>, package <span class="op">=</span> <span class="st">"treeio"</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.mega</a></span><span class="op">(</span><span class="va">file</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_range.html">geom_range</a></span><span class="op">(</span><span class="st">'reltime_0.95_CI'</span>, color<span class="op">=</span><span class="st">'red'</span>, size<span class="op">=</span><span class="fl">3</span>, alpha<span class="op">=</span><span class="fl">.3</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_range.html">geom_range</a></span><span class="op">(</span><span class="st">'reltime_0.95_CI'</span>, color<span class="op">=</span><span class="st">'red'</span>, size<span class="op">=</span><span class="fl">3</span>, alpha<span class="op">=</span><span class="fl">.3</span>, center<span class="op">=</span><span class="st">'reltime'</span><span class="op">)</span>  
<span class="va">p3</span> <span class="op">&lt;-</span> <span class="va">p2</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/scale_x_range.html">scale_x_range</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,<span class="va">p3</span>,ncol <span class="op">=</span> <span class="fl">3</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/33.png"></div>
</div>
<div id="其他软件输出的结果" class="section level5" number="3.2.6.2.5">
<h5>
<span class="header-section-number">3.2.6.2.5</span> 其他软件输出的结果<a class="anchor" aria-label="anchor" href="#%E5%85%B6%E4%BB%96%E8%BD%AF%E4%BB%B6%E8%BE%93%E5%87%BA%E7%9A%84%E7%BB%93%E6%9E%9C"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb64"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">rstfile</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/PAML_Codeml"</span>, <span class="st">"rst"</span>, 
                       package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">mlcfile</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata/PAML_Codeml"</span>, <span class="st">"mlc"</span>, 
                       package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>

<span class="va">ml</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/read.codeml.html">read.codeml</a></span><span class="op">(</span><span class="va">rstfile</span>, <span class="va">mlcfile</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">ml</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">dN_vs_dS</span><span class="op">)</span>, branch.length<span class="op">=</span><span class="st">'dN_vs_dS'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_colour_continuous.html">scale_color_continuous</a></span><span class="op">(</span>name<span class="op">=</span><span class="st">'dN/dS'</span>, limits<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1.5</span><span class="op">)</span>,
                         oob<span class="op">=</span><span class="fu">scales</span><span class="fu">::</span><span class="va"><a href="https://scales.r-lib.org/reference/oob.html">squish</a></span>,
                         low<span class="op">=</span><span class="st">'darkgreen'</span>, high<span class="op">=</span><span class="st">'red'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_text</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">branch</span>, label<span class="op">=</span><span class="va">AA_subs</span><span class="op">)</span>, 
            vjust<span class="op">=</span><span class="op">-</span><span class="fl">.5</span>, color<span class="op">=</span><span class="st">'steelblue'</span>, size<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.9</span>, <span class="fl">.3</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/34.png"></div>
</div>
<div id="放大特定区域" class="section level5" number="3.2.6.2.6">
<h5>
<span class="header-section-number">3.2.6.2.6</span> 放大特定区域<a class="anchor" aria-label="anchor" href="#%E6%94%BE%E5%A4%A7%E7%89%B9%E5%AE%9A%E5%8C%BA%E5%9F%9F"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb65"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="va">nwk</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"sample.nwk"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">nwk</span><span class="op">)</span>
<span class="va">p1</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/viewClade.html">viewClade</a></span><span class="op">(</span><span class="va">p</span>, <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/MRCA.html">MRCA</a></span><span class="op">(</span><span class="va">p</span>, <span class="st">"I"</span>, <span class="st">"L"</span><span class="op">)</span><span class="op">)</span>

<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">p1</span>,<span class="va">p2</span>,ncol <span class="op">=</span> <span class="fl">2</span>, labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">'原图'</span>特<span class="st">'特定区域'</span>NA<span class="op">)</span>NA<span class="op">)</span>NA</code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/35.png"></div>
</div>
<div id="标准化选中的clade" class="section level5" number="3.2.6.2.7">
<h5>
<span class="header-section-number">3.2.6.2.7</span> 标准化选中的clade<a class="anchor" aria-label="anchor" href="#%E6%A0%87%E5%87%86%E5%8C%96%E9%80%89%E4%B8%AD%E7%9A%84clade"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb66"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">tree2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupClade.html">groupClade</a></span><span class="op">(</span><span class="va">tree</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">17</span>, <span class="fl">21</span><span class="op">)</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">'none'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>, <span class="st">"firebrick"</span>, <span class="st">"steelblue"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/scaleClade.html">scaleClade</a></span><span class="op">(</span><span class="va">p</span>, node<span class="op">=</span><span class="fl">17</span>, scale<span class="op">=</span><span class="fl">.1</span><span class="op">)</span> </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/36.png"></div>
</div>
<div id="隐藏展示某个clade" class="section level5" number="3.2.6.2.8">
<h5>
<span class="header-section-number">3.2.6.2.8</span> 隐藏/展示某个clade<a class="anchor" aria-label="anchor" href="#%E9%9A%90%E8%97%8F%E5%B1%95%E7%A4%BA%E6%9F%90%E4%B8%AAclade"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb67"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">21</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_point2.html">geom_point2</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>subset<span class="op">=</span><span class="op">(</span><span class="va">node</span><span class="op">==</span><span class="fl">21</span><span class="op">)</span><span class="op">)</span>, shape<span class="op">=</span><span class="fl">21</span>, size<span class="op">=</span><span class="fl">5</span>, fill<span class="op">=</span><span class="st">'green'</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="va">p2</span>, node<span class="op">=</span><span class="fl">23</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_point2.html">geom_point2</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>subset<span class="op">=</span><span class="op">(</span><span class="va">node</span><span class="op">==</span><span class="fl">23</span><span class="op">)</span><span class="op">)</span>, shape<span class="op">=</span><span class="fl">23</span>, size<span class="op">=</span><span class="fl">5</span>, fill<span class="op">=</span><span class="st">'red'</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">p2</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/expand.html">expand</a></span><span class="op">(</span><span class="va">p2</span>, node<span class="op">=</span><span class="fl">23</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/expand.html">expand</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">21</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/37.png"></div>
</div>
<div id="利用三角形隐藏展示某个clade" class="section level5" number="3.2.6.2.9">
<h5>
<span class="header-section-number">3.2.6.2.9</span> 利用三角形隐藏/展示某个clade<a class="anchor" aria-label="anchor" href="#%E5%88%A9%E7%94%A8%E4%B8%89%E8%A7%92%E5%BD%A2%E9%9A%90%E8%97%8F%E5%B1%95%E7%A4%BA%E6%9F%90%E4%B8%AAclade"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb68"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">node</span> <span class="op">&lt;-</span> <span class="fl">21</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="va">p2</span>, <span class="va">node</span>, <span class="st">'max'</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/expand.html">expand</a></span><span class="op">(</span><span class="va">node</span><span class="op">)</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="va">p2</span>, <span class="va">node</span>, <span class="st">'min'</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/expand.html">expand</a></span><span class="op">(</span><span class="va">node</span><span class="op">)</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="va">p2</span>, <span class="va">node</span>, <span class="st">'mixed'</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/expand.html">expand</a></span><span class="op">(</span><span class="va">node</span><span class="op">)</span>

<span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="va">p</span>, <span class="fl">21</span>, <span class="st">'mixed'</span>, fill<span class="op">=</span><span class="st">'steelblue'</span>, alpha<span class="op">=</span><span class="fl">.4</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="fl">23</span>, <span class="st">'mixed'</span>, fill<span class="op">=</span><span class="st">'firebrick'</span>, color<span class="op">=</span><span class="st">'blue'</span><span class="op">)</span>
  
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/scaleClade.html">scaleClade</a></span><span class="op">(</span><span class="va">p</span>, <span class="fl">23</span>, <span class="fl">.2</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/compute.html">collapse</a></span><span class="op">(</span><span class="fl">23</span>, <span class="st">'min'</span>, fill<span class="op">=</span><span class="st">"darkgreen"</span><span class="op">)</span>  </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/38.png"></div>
</div>
<div id="分组taxa" class="section level5" number="3.2.6.2.10">
<h5>
<span class="header-section-number">3.2.6.2.10</span> 分组Taxa<a class="anchor" aria-label="anchor" href="#%E5%88%86%E7%BB%84taxa"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb69"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://nanx.me/ggsci/">ggsci</a></span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">iris</span><span class="op">)</span>
<span class="va">rn</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="va">iris</span><span class="op">[</span>,<span class="fl">5</span><span class="op">]</span>, <span class="st">"_"</span>, <span class="fl">1</span><span class="op">:</span><span class="fl">150</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">rownames</a></span><span class="op">(</span><span class="va">iris</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">rn</span>
<span class="va">d_iris</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/dist.html">dist</a></span><span class="op">(</span><span class="va">iris</span><span class="op">[</span>,<span class="op">-</span><span class="fl">5</span><span class="op">]</span>, method<span class="op">=</span><span class="st">"man"</span><span class="op">)</span>

<span class="va">c</span> <span class="op">&lt;-</span> <span class="fu">ape</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/bionj.html">bionj</a></span><span class="op">(</span><span class="va">d_iris</span><span class="op">)</span>
<span class="va">grp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>setosa     <span class="op">=</span> <span class="va">rn</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">50</span><span class="op">]</span>,
            versicolor <span class="op">=</span> <span class="va">rn</span><span class="op">[</span><span class="fl">51</span><span class="op">:</span><span class="fl">100</span><span class="op">]</span>,
            virginica  <span class="op">=</span> <span class="va">rn</span><span class="op">[</span><span class="fl">101</span><span class="op">:</span><span class="fl">150</span><span class="op">]</span><span class="op">)</span>

<span class="va">p_iris</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree_iris</span>, layout <span class="op">=</span> <span class="st">'circular'</span>, branch.length<span class="op">=</span><span class="st">'none'</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">p_iris</span>, <span class="va">grp</span>, <span class="st">'group'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">group</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://nanx.me/ggsci/reference/scale_aaas.html">scale_color_aaas</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">"right"</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/39.png"></div>
<p>另外一种方法：</p>
<div class="sourceCode" id="cb70"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">tree_iris</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">tree_iris</span>, <span class="va">grp</span>, <span class="st">"Species"</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree_iris</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">Species</span><span class="op">)</span>, layout <span class="op">=</span> <span class="st">'circular'</span>, branch.length <span class="op">=</span> <span class="st">'none'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">"right"</span><span class="op">)</span></code></pre></div>
</div>
<div id="旋转clade" class="section level5" number="3.2.6.2.11">
<h5>
<span class="header-section-number">3.2.6.2.11</span> 旋转clade<a class="anchor" aria-label="anchor" href="#%E6%97%8B%E8%BD%ACclade"><i class="fas fa-link"></i></a>
</h5>
<p>旋转<code>clade</code>有两种方法：</p>
<div class="sourceCode" id="cb71"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_point2.html">geom_point2</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>subset<span class="op">=</span><span class="va">node</span><span class="op">==</span><span class="fl">16</span><span class="op">)</span>, color<span class="op">=</span><span class="st">'darkgreen'</span>, size<span class="op">=</span><span class="fl">5</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="va">p1</span>, <span class="fl">17</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate.html">rotate</a></span><span class="op">(</span><span class="fl">21</span><span class="op">)</span> <span class="co"># 方法1</span>NA<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/flip.html">flip</a></span><span class="op">(</span><span class="va">p2</span>, <span class="fl">17</span>, <span class="fl">21</span><span class="op">)</span> <span class="co"># 方法2</span>NA</code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/40.png"></div>
</div>
<div id="外部数据mapping到树上" class="section level5" number="3.2.6.2.12">
<h5>
<span class="header-section-number">3.2.6.2.12</span> 外部数据mapping到树上<a class="anchor" aria-label="anchor" href="#%E5%A4%96%E9%83%A8%E6%95%B0%E6%8D%AEmapping%E5%88%B0%E6%A0%91%E4%B8%8A"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb72"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/GuangchuangYu/ggimage">ggimage</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>

<span class="co"># 文件下载地址</span>NA<span class="co"># https://raw.githubusercontent.com/YuLab-SMU/treedata-book/master/data/tree_boots.nwk</span>
<span class="co"># https://raw.githubusercontent.com/YuLab-SMU/treedata-book/master/data/tip_data.csv</span>


<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"tree_boots.nwk"</span><span class="op">)</span>
<span class="va">info</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"tip_data.csv"</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">%&lt;+%</span> <span class="va">info</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="op">-</span><span class="fl">.1</span>, <span class="fl">4</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>offset <span class="op">=</span> <span class="fl">.6</span>, hjust <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>shape <span class="op">=</span> <span class="va">trophic_habit</span>, color <span class="op">=</span> <span class="va">trophic_habit</span>, size <span class="op">=</span> <span class="va">mass_in_kg</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_size.html">scale_size_continuous</a></span><span class="op">(</span>range <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">3</span>, <span class="fl">10</span><span class="op">)</span><span class="op">)</span>

<span class="co">#https://raw.githubusercontent.com/YuLab-SMU/treedata-book/master/data/inode_data.csv</span>
<span class="va">d2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"inode_data.csv"</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">%&lt;+%</span> <span class="va">d2</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_label</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label <span class="op">=</span> <span class="va">vernacularName.y</span>, fill <span class="op">=</span> <span class="va">posterior</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_gradient.html">scale_fill_gradientn</a></span><span class="op">(</span>colors <span class="op">=</span> <span class="fu">RColorBrewer</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/RColorBrewer/man/ColorBrewer.html">brewer.pal</a></span><span class="op">(</span><span class="fl">3</span>, <span class="st">"YlGnBu"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/41.png"></div>
</div>
<div id="将snp数据添加到树上" class="section level5" number="3.2.6.2.13">
<h5>
<span class="header-section-number">3.2.6.2.13</span> 将SNP数据添加到树上<a class="anchor" aria-label="anchor" href="#%E5%B0%86snp%E6%95%B0%E6%8D%AE%E6%B7%BB%E5%8A%A0%E5%88%B0%E6%A0%91%E4%B8%8A"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb73"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="co">## remote_folder &lt;- paste0("https://raw.githubusercontent.com/katholt/",</span>
<span class="co">##                         "plotTree/master/tree_example_april2015/")</span>
<span class="va">remote_folder</span> <span class="op">&lt;-</span> <span class="st">"data/tree_example_april2015/"</span> 

<span class="co">## read the phylogenetic tree</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="va">remote_folder</span>, <span class="st">"tree.nwk"</span><span class="op">)</span><span class="op">)</span>

<span class="co">## read the sampling information data set</span>
<span class="va">info</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="va">remote_folder</span>,<span class="st">"info.csv"</span><span class="op">)</span><span class="op">)</span>

<span class="co">## read and process the allele table</span>
<span class="va">snps</span><span class="op">&lt;-</span><span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="va">remote_folder</span>, <span class="st">"alleles.csv"</span><span class="op">)</span>, header <span class="op">=</span> <span class="cn">F</span>,
                row.names <span class="op">=</span> <span class="fl">1</span>, stringsAsFactor <span class="op">=</span> <span class="cn">F</span><span class="op">)</span>
<span class="va">snps_strainCols</span> <span class="op">&lt;-</span> <span class="va">snps</span><span class="op">[</span><span class="fl">1</span>,<span class="op">]</span> 
<span class="va">snps</span><span class="op">&lt;-</span><span class="va">snps</span><span class="op">[</span><span class="op">-</span><span class="fl">1</span>,<span class="op">]</span> <span class="co"># drop strain names</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op">(</span><span class="va">snps</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">snps_strainCols</span>

<span class="va">gapChar</span> <span class="op">&lt;-</span> <span class="st">"?"</span>
<span class="va">snp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/transpose-methods.html">t</a></span><span class="op">(</span><span class="va">snps</span><span class="op">)</span>
<span class="va">lsnp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/apply.html">apply</a></span><span class="op">(</span><span class="va">snp</span>, <span class="fl">1</span>, <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">{</span>
        <span class="va">x</span> <span class="op">!=</span> <span class="va">snp</span><span class="op">[</span><span class="fl">1</span>,<span class="op">]</span> <span class="op">&amp;</span> <span class="va">x</span> <span class="op">!=</span> <span class="va">gapChar</span> <span class="op">&amp;</span> <span class="va">snp</span><span class="op">[</span><span class="fl">1</span>,<span class="op">]</span> <span class="op">!=</span> <span class="va">gapChar</span>
    <span class="op">}</span><span class="op">)</span>
<span class="va">lsnp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/as.data.frame.html">as.data.frame</a></span><span class="op">(</span><span class="va">lsnp</span><span class="op">)</span>
<span class="va">lsnp</span><span class="op">$</span><span class="va">pos</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/colnames.html">rownames</a></span><span class="op">(</span><span class="va">lsnp</span><span class="op">)</span><span class="op">)</span>
<span class="va">lsnp</span> <span class="op">&lt;-</span> <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/gather.html">gather</a></span><span class="op">(</span><span class="va">lsnp</span>, <span class="va">name</span>, <span class="va">value</span>, <span class="op">-</span><span class="va">pos</span><span class="op">)</span>
<span class="va">snp_data</span> <span class="op">&lt;-</span> <span class="va">lsnp</span><span class="op">[</span><span class="va">lsnp</span><span class="op">$</span><span class="va">value</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"name"</span>, <span class="st">"pos"</span><span class="op">)</span><span class="op">]</span>

<span class="co">## read the trait data</span>
<span class="va">bar_data</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="va">remote_folder</span>, <span class="st">"bar.csv"</span><span class="op">)</span><span class="op">)</span>

<span class="co">## visualize the tree </span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> 

<span class="co">## attach the sampling information data set </span>
<span class="co">## and add symbols colored by location</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">%&lt;+%</span> <span class="va">info</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">location</span><span class="op">)</span><span class="op">)</span>

<span class="co">## visualize SNP and Trait data using dot and bar charts,</span>
<span class="co">## and align them based on tree structure</span>
<span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/facet-plot.html">geom_facet</a></span><span class="op">(</span>panel <span class="op">=</span> <span class="st">"SNP"</span>, data <span class="op">=</span> <span class="va">snp_data</span>, geom <span class="op">=</span> <span class="va">geom_point</span>, 
               mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">pos</span>, color <span class="op">=</span> <span class="va">location</span><span class="op">)</span>, shape <span class="op">=</span> <span class="st">'|'</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/facet-plot.html">geom_facet</a></span><span class="op">(</span>panel <span class="op">=</span> <span class="st">"Trait"</span>, data <span class="op">=</span> <span class="va">bar_data</span>, geom <span class="op">=</span> <span class="fu">ggstance</span><span class="fu">::</span><span class="va"><a href="https://rdrr.io/pkg/ggstance/man/geom_barh.html">geom_barh</a></span>, 
                <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">dummy_bar_value</span>, color <span class="op">=</span> <span class="va">location</span>, fill <span class="op">=</span> <span class="va">location</span><span class="op">)</span>, 
                stat <span class="op">=</span> <span class="st">"identity"</span>, width <span class="op">=</span> <span class="fl">.6</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.05</span>, <span class="fl">.85</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/42.png"></div>
</div>
<div id="关联矩阵" class="section level5" number="3.2.6.2.14">
<h5>
<span class="header-section-number">3.2.6.2.14</span> 关联矩阵<a class="anchor" aria-label="anchor" href="#%E5%85%B3%E8%81%94%E7%9F%A9%E9%98%B5"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb74"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">beast_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/MCC_FluA_H3.tree"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">beast_tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://docs.ropensci.org/treeio/reference/beast-parser.html">read.beast</a></span><span class="op">(</span><span class="va">beast_file</span><span class="op">)</span>

<span class="va">genotype_file</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples/Genotype.txt"</span>, package<span class="op">=</span><span class="st">"ggtree"</span><span class="op">)</span>
<span class="va">genotype</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.table</a></span><span class="op">(</span><span class="va">genotype_file</span>, sep<span class="op">=</span><span class="st">"\t"</span>, stringsAsFactor<span class="op">=</span><span class="cn">F</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op">(</span><span class="va">genotype</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/grep.html">sub</a></span><span class="op">(</span><span class="st">"\\.$"</span>, <span class="st">""</span>, <span class="fu"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op">(</span><span class="va">genotype</span><span class="op">)</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree</span>, mrsd<span class="op">=</span><span class="st">"2013-01-01"</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale</a></span><span class="op">(</span>x<span class="op">=</span><span class="fl">2008</span>, y<span class="op">=</span><span class="fl">1</span>, offset<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">2</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/gheatmap.html">gheatmap</a></span><span class="op">(</span><span class="va">p</span>, <span class="va">genotype</span>, offset<span class="op">=</span><span class="fl">5</span>, width<span class="op">=</span><span class="fl">0.5</span>, font.size<span class="op">=</span><span class="fl">3</span>, 
        colnames_angle<span class="op">=</span><span class="op">-</span><span class="fl">45</span>, hjust<span class="op">=</span><span class="fl">0</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>breaks<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"HuH3N2"</span>, <span class="st">"pdm"</span>, <span class="st">"trig"</span><span class="op">)</span>, 
        values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"steelblue"</span>, <span class="st">"firebrick"</span>, <span class="st">"darkgreen"</span><span class="op">)</span>, name<span class="op">=</span><span class="st">"genotype"</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">beast_tree</span>, mrsd<span class="op">=</span><span class="st">"2013-01-01"</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">2</span>, align<span class="op">=</span><span class="cn">TRUE</span>, linesize<span class="op">=</span><span class="fl">.5</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/gheatmap.html">gheatmap</a></span><span class="op">(</span><span class="va">p</span>, <span class="va">genotype</span>, offset<span class="op">=</span><span class="fl">8</span>, width<span class="op">=</span><span class="fl">0.6</span>, 
        colnames<span class="op">=</span><span class="cn">FALSE</span>, legend_title<span class="op">=</span><span class="st">"genotype"</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/scale_x_ggtree.html">scale_x_ggtree</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>expand<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">0.3</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/43.png"></div>
</div>
<div id="关联多个矩阵" class="section level5" number="3.2.6.2.15">
<h5>
<span class="header-section-number">3.2.6.2.15</span> 关联多个矩阵<a class="anchor" aria-label="anchor" href="#%E5%85%B3%E8%81%94%E5%A4%9A%E4%B8%AA%E7%9F%A9%E9%98%B5"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb75"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">nwk</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"sample.nwk"</span>, package<span class="op">=</span><span class="st">"treeio"</span><span class="op">)</span>

<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">nwk</span><span class="op">)</span>
<span class="va">circ</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout <span class="op">=</span> <span class="st">"circular"</span><span class="op">)</span>

<span class="va">df</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>first<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"a"</span>, <span class="st">"c"</span>, <span class="st">"d"</span>, <span class="st">"d"</span>, <span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"e"</span>, <span class="st">"e"</span>, <span class="st">"f"</span>, <span class="st">"c"</span>, <span class="st">"f"</span><span class="op">)</span>,
                 second<span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"z"</span>, <span class="st">"z"</span>, <span class="st">"z"</span>, <span class="st">"z"</span>, <span class="st">"y"</span>, <span class="st">"y"</span>, <span class="st">"y"</span>, <span class="st">"y"</span>, <span class="st">"x"</span>, <span class="st">"x"</span>, <span class="st">"x"</span>, <span class="st">"a"</span>, <span class="st">"a"</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">rownames</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">tree</span><span class="op">$</span><span class="va">tip.label</span>

<span class="va">df2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/as.data.frame.html">as.data.frame</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/matrix.html">matrix</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">39</span><span class="op">)</span>, ncol<span class="op">=</span><span class="fl">3</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">rownames</a></span><span class="op">(</span><span class="va">df2</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">tree</span><span class="op">$</span><span class="va">tip.label</span>
<span class="fu"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op">(</span><span class="va">df2</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">LETTERS</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">3</span><span class="op">]</span>


<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/gheatmap.html">gheatmap</a></span><span class="op">(</span><span class="va">circ</span>, <span class="va">df</span>, offset<span class="op">=</span><span class="fl">.8</span>, width<span class="op">=</span><span class="fl">.2</span>,
               colnames_angle<span class="op">=</span><span class="fl">95</span>, colnames_offset_y <span class="op">=</span> <span class="fl">.25</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_viridis.html">scale_fill_viridis_d</a></span><span class="op">(</span>option<span class="op">=</span><span class="st">"D"</span>, name<span class="op">=</span><span class="st">"discrete\nvalue"</span><span class="op">)</span>


<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va">ggnewscale</span><span class="op">)</span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">p1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggnewscale/man/new_scale.html">new_scale_fill</a></span><span class="op">(</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/gheatmap.html">gheatmap</a></span><span class="op">(</span><span class="va">p2</span>, <span class="va">df2</span>, offset<span class="op">=</span><span class="fl">15</span>, width<span class="op">=</span><span class="fl">.3</span>,
         colnames_angle<span class="op">=</span><span class="fl">90</span>, colnames_offset_y <span class="op">=</span> <span class="fl">.25</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_viridis.html">scale_fill_viridis_c</a></span><span class="op">(</span>option<span class="op">=</span><span class="st">"A"</span>, name<span class="op">=</span><span class="st">"continuous\nvalue"</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/44.png"></div>
</div>
<div id="多序列比对可视化" class="section level5" number="3.2.6.2.16">
<h5>
<span class="header-section-number">3.2.6.2.16</span> 多序列比对可视化<a class="anchor" aria-label="anchor" href="#%E5%A4%9A%E5%BA%8F%E5%88%97%E6%AF%94%E5%AF%B9%E5%8F%AF%E8%A7%86%E5%8C%96"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb76"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"data/tree.nwk"</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">3</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/msaplot.html">msaplot</a></span><span class="op">(</span><span class="va">p</span>, <span class="st">"data/sequence.fasta"</span>, offset<span class="op">=</span><span class="fl">3</span>, width<span class="op">=</span><span class="fl">2</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">'circular'</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>offset<span class="op">=</span><span class="fl">4</span>, align<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">12</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/msaplot.html">msaplot</a></span><span class="op">(</span><span class="va">p</span>, <span class="st">"data/sequence.fasta"</span>, window<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">120</span>, <span class="fl">200</span><span class="op">)</span><span class="op">)</span>  </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/45.png"></div>
</div>
<div id="拼图" class="section level5" number="3.2.6.2.17">
<h5>
<span class="header-section-number">3.2.6.2.17</span> 拼图<a class="anchor" aria-label="anchor" href="#%E6%8B%BC%E5%9B%BE"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb77"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">2019</span><span class="op">-</span><span class="fl">10</span><span class="op">-</span><span class="fl">31</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">10</span><span class="op">)</span>

<span class="va">d1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>
    <span class="co"># only some labels match</span>
    label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="va">tr</span><span class="op">$</span><span class="va">tip.label</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op">(</span><span class="fl">5</span>, <span class="fl">5</span><span class="op">)</span><span class="op">]</span>, <span class="st">"A"</span><span class="op">)</span>,
    value <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op">(</span><span class="fl">1</span><span class="op">:</span><span class="fl">6</span>, <span class="fl">6</span><span class="op">)</span><span class="op">)</span>

<span class="va">d2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>
    label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="va">tr</span><span class="op">$</span><span class="va">tip.label</span>, <span class="fl">5</span><span class="op">)</span>,
    category <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="va">LETTERS</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">5</span><span class="op">]</span>, each<span class="op">=</span><span class="fl">10</span><span class="op">)</span>,
    value <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">50</span>, <span class="fl">0</span>, <span class="fl">3</span><span class="op">)</span><span class="op">)</span> 

<span class="va">g</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tr</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>align<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">d1</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span><span class="va">label</span>, <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_bar.html">geom_col</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="va">label</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_text.html">geom_text</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">label</span>, y<span class="op">=</span> <span class="va">value</span><span class="op">+</span><span class="fl">.1</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/coord_flip.html">coord_flip</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="st">'none'</span><span class="op">)</span>
 
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">d2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">category</span>, y<span class="op">=</span><span class="va">label</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/geom_tile.html">geom_tile</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_viridis.html">scale_fill_viridis_c</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_tree2.html">theme_tree2</a></span><span class="op">(</span><span class="op">)</span> 
    
<span class="fu">cowplot</span><span class="fu">::</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/plot_grid.html">plot_grid</a></span><span class="op">(</span><span class="va">g</span>, <span class="va">p2</span>, <span class="va">p1</span>, ncol<span class="op">=</span><span class="fl">3</span><span class="op">)</span> 

<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/aplot">aplot</a></span><span class="op">)</span>
<span class="va">p2</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/aplot/man/plot-insertion.html">insert_left</a></span><span class="op">(</span><span class="va">g</span><span class="op">)</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/aplot/man/plot-insertion.html">insert_right</a></span><span class="op">(</span><span class="va">p1</span>, width<span class="op">=</span><span class="fl">.5</span><span class="op">)</span> </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/46.png"></div>
</div>
<div id="图片注释" class="section level5" number="3.2.6.2.18">
<h5>
<span class="header-section-number">3.2.6.2.18</span> 图片注释<a class="anchor" aria-label="anchor" href="#%E5%9B%BE%E7%89%87%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h5>
<p>没能找到图片下载地址，看代码很容易理解：</p>
<div class="sourceCode" id="cb78"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/GuangchuangYu/ggimage">ggimage</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>

<span class="va">nwk</span> <span class="op">&lt;-</span> <span class="st">"((((bufonidae, dendrobatidae), ceratophryidae), (centrolenidae, leptodactylidae)), hylidae);"</span>

<span class="va">x</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text <span class="op">=</span> <span class="va">nwk</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">7</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">ylim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">6.2</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>image<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="st">"img/frogs/"</span>, <span class="va">label</span>, <span class="st">'.jpg'</span><span class="op">)</span><span class="op">)</span>, 
              geom<span class="op">=</span><span class="st">"image"</span>, offset<span class="op">=</span><span class="fl">2</span>, align<span class="op">=</span><span class="fl">2</span>, size<span class="op">=</span><span class="fl">.2</span><span class="op">)</span>  <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>geom<span class="op">=</span><span class="st">'label'</span>, offset<span class="op">=</span><span class="fl">1</span>, hjust<span class="op">=</span><span class="fl">.5</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggimage/man/geom_image.html">geom_image</a></span><span class="op">(</span>x<span class="op">=</span><span class="fl">.8</span>, y<span class="op">=</span><span class="fl">5.5</span>, image<span class="op">=</span><span class="st">"img/frogs/frog.jpg"</span>, size<span class="op">=</span><span class="fl">.2</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/47.png"></div>
</div>
<div id="图标剪影注释" class="section level5" number="3.2.6.2.19">
<h5>
<span class="header-section-number">3.2.6.2.19</span> 图标（剪影）注释<a class="anchor" aria-label="anchor" href="#%E5%9B%BE%E6%A0%87%E5%89%AA%E5%BD%B1%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h5>
<p><a href="http://phylopic.org/">PhyloPic</a>提供了1300余种生物剪影，<code>ggtree</code>能够调用这个数据库中的生物图标进行注释，相当于上面的图片。这种情况下绘图稍微有点慢，毕竟这个数据库是国外的。</p>
<div class="sourceCode" id="cb79"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="va">newick</span> <span class="op">&lt;-</span> <span class="st">"((Pongo_abelii,(Gorilla_gorilla_gorilla,(Pan_paniscus,Pan_troglodytes)Pan,Homo_sapiens)Homininae)Hominidae,Nomascus_leucogenys)Hominoidea;"</span>

<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text<span class="op">=</span><span class="va">newick</span><span class="op">)</span>

<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu">ggimage</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggimage/man/phylopic_uid.html">phylopic_uid</a></span><span class="op">(</span><span class="va">tree</span><span class="op">$</span><span class="va">tip.label</span><span class="op">)</span>
<span class="va">d</span><span class="op">$</span><span class="va">body_mass</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">52</span>, <span class="fl">114</span>, <span class="fl">47</span>, <span class="fl">45</span>, <span class="fl">58</span>, <span class="fl">6</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span> <span class="op">%&lt;+%</span> <span class="va">d</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>image<span class="op">=</span><span class="va">uid</span>, colour<span class="op">=</span><span class="va">body_mass</span><span class="op">)</span>, geom<span class="op">=</span><span class="st">"phylopic"</span>, offset<span class="op">=</span><span class="fl">2.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">label</span><span class="op">)</span>, offset <span class="op">=</span> <span class="fl">.2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">7</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_viridis.html">scale_color_viridis_c</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/48.png"></div>
</div>
<div id="使用子图进行注释" class="section level5" number="3.2.6.2.20">
<h5>
<span class="header-section-number">3.2.6.2.20</span> 使用子图进行注释<a class="anchor" aria-label="anchor" href="#%E4%BD%BF%E7%94%A8%E5%AD%90%E5%9B%BE%E8%BF%9B%E8%A1%8C%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h5>
<p>用子图进行注释是通过函数<code><a href="https://rdrr.io/pkg/ggtree/man/inset.html">geom_inset()</a></code>来完成的。</p>
<div id="条形图注释" class="section level6" number="3.2.6.2.20.1">
<h6>
<span class="header-section-number">3.2.6.2.20.1</span> 条形图注释<a class="anchor" aria-label="anchor" href="#%E6%9D%A1%E5%BD%A2%E5%9B%BE%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h6>
<div class="sourceCode" id="cb80"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/liamrevell/phytools">phytools</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">anoletree</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phytools/man/getStates.html">getStates</a></span><span class="op">(</span><span class="va">anoletree</span>,<span class="st">"tips"</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="va">anoletree</span>

<span class="va">cols</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/setNames.html">setNames</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/grDevices/palette.html">palette</a></span><span class="op">(</span><span class="op">)</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/unique.html">unique</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">]</span>,<span class="fu"><a href="https://rdrr.io/r/base/sort.html">sort</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/unique.html">unique</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="va">fitER</span> <span class="op">&lt;-</span> <span class="fu">ape</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/ace.html">ace</a></span><span class="op">(</span><span class="va">x</span>,<span class="va">tree</span>,model<span class="op">=</span><span class="st">"ER"</span>,type<span class="op">=</span><span class="st">"discrete"</span><span class="op">)</span>
<span class="va">ancstats</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/as.data.frame.html">as.data.frame</a></span><span class="op">(</span><span class="va">fitER</span><span class="op">$</span><span class="va">lik.anc</span><span class="op">)</span>
<span class="va">ancstats</span><span class="op">$</span><span class="va">node</span> <span class="op">&lt;-</span> <span class="fl">1</span><span class="op">:</span><span class="va">tree</span><span class="op">$</span><span class="va">Nnode</span><span class="op">+</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/summary.phylo.html">Ntip</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span>

<span class="co">## cols parameter indicate which columns store stats</span>
<span class="va">bars</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/nodebar.html">nodebar</a></span><span class="op">(</span><span class="va">ancstats</span>, cols<span class="op">=</span><span class="fl">1</span><span class="op">:</span><span class="fl">6</span><span class="op">)</span>
<span class="va">bars</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">bars</span>, <span class="kw">function</span><span class="op">(</span><span class="va">g</span><span class="op">)</span> <span class="va">g</span><span class="op">+</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="va">cols</span><span class="op">)</span><span class="op">)</span>

<span class="va">tree2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">tree</span>, <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>, stat <span class="op">=</span> <span class="va">x</span> <span class="op">)</span>, by <span class="op">=</span> <span class="st">'label'</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color <span class="op">=</span> <span class="va">stat</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="va">cols</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">8</span><span class="op">)</span>
<span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/inset.html">geom_inset</a></span><span class="op">(</span><span class="va">bars</span>, width <span class="op">=</span> <span class="fl">.08</span>, height <span class="op">=</span> <span class="fl">.05</span>, x <span class="op">=</span> <span class="st">"branch"</span><span class="op">)</span> </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/49.png"></div>
</div>
<div id="饼图注释" class="section level6" number="3.2.6.2.20.2">
<h6>
<span class="header-section-number">3.2.6.2.20.2</span> 饼图注释<a class="anchor" aria-label="anchor" href="#%E9%A5%BC%E5%9B%BE%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h6>
<div class="sourceCode" id="cb81"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/liamrevell/phytools">phytools</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">anoletree</span><span class="op">)</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phytools/man/getStates.html">getStates</a></span><span class="op">(</span><span class="va">anoletree</span>,<span class="st">"tips"</span><span class="op">)</span>
<span class="va">tree</span> <span class="op">&lt;-</span> <span class="va">anoletree</span>

<span class="va">cols</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/setNames.html">setNames</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/grDevices/palette.html">palette</a></span><span class="op">(</span><span class="op">)</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/unique.html">unique</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">]</span>,<span class="fu"><a href="https://rdrr.io/r/base/sort.html">sort</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/unique.html">unique</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="va">fitER</span> <span class="op">&lt;-</span> <span class="fu">ape</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/ace.html">ace</a></span><span class="op">(</span><span class="va">x</span>,<span class="va">tree</span>,model<span class="op">=</span><span class="st">"ER"</span>,type<span class="op">=</span><span class="st">"discrete"</span><span class="op">)</span>
<span class="va">ancstats</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/as.data.frame.html">as.data.frame</a></span><span class="op">(</span><span class="va">fitER</span><span class="op">$</span><span class="va">lik.anc</span><span class="op">)</span>
<span class="va">ancstats</span><span class="op">$</span><span class="va">node</span> <span class="op">&lt;-</span> <span class="fl">1</span><span class="op">:</span><span class="va">tree</span><span class="op">$</span><span class="va">Nnode</span><span class="op">+</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/summary.phylo.html">Ntip</a></span><span class="op">(</span><span class="va">tree</span><span class="op">)</span>

<span class="va">pies</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/nodepie.html">nodepie</a></span><span class="op">(</span><span class="va">ancstats</span>, cols <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">6</span><span class="op">)</span>
<span class="va">pies</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">pies</span>, <span class="kw">function</span><span class="op">(</span><span class="va">g</span><span class="op">)</span> <span class="va">g</span><span class="op">+</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="va">cols</span><span class="op">)</span><span class="op">)</span>

<span class="va">tree2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html">full_join</a></span><span class="op">(</span><span class="va">tree</span>, <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>, stat <span class="op">=</span> <span class="va">x</span> <span class="op">)</span>, by <span class="op">=</span> <span class="st">'label'</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color <span class="op">=</span> <span class="va">stat</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_color_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="va">cols</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">8</span><span class="op">)</span>

<span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/inset.html">geom_inset</a></span><span class="op">(</span><span class="va">pies</span>, width <span class="op">=</span> <span class="fl">.1</span>, height <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> </code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/50.png"></div>
</div>
<div id="多种图像组合注释" class="section level6" number="3.2.6.2.20.3">
<h6>
<span class="header-section-number">3.2.6.2.20.3</span> 多种图像组合注释<a class="anchor" aria-label="anchor" href="#%E5%A4%9A%E7%A7%8D%E5%9B%BE%E5%83%8F%E7%BB%84%E5%90%88%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h6>
<div class="sourceCode" id="cb82"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">pies_and_bars</span> <span class="op">&lt;-</span> <span class="va">pies</span>
<span class="va">i</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">pies</span><span class="op">)</span>, <span class="fl">20</span><span class="op">)</span>
<span class="va">pies_and_bars</span><span class="op">[</span><span class="va">i</span><span class="op">]</span> <span class="op">&lt;-</span> <span class="va">bars</span><span class="op">[</span><span class="va">i</span><span class="op">]</span>
<span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/inset.html">geom_inset</a></span><span class="op">(</span><span class="va">pies_and_bars</span>, width<span class="op">=</span><span class="fl">.08</span>, height<span class="op">=</span><span class="fl">.05</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/51.png"></div>
</div>
</div>
<div id="phylomoji注释" class="section level5" number="3.2.6.2.21">
<h5>
<span class="header-section-number">3.2.6.2.21</span> <code>Phylomoji</code>注释<a class="anchor" aria-label="anchor" href="#phylomoji%E6%B3%A8%E9%87%8A"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb83"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="co"># install.packages('emojifont')</span>

<span class="va">tt</span> <span class="op">=</span> <span class="st">'((snail,mushroom),(((sunflower,evergreen_tree),leaves),green_salad));'</span>
<span class="va">tree</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text <span class="op">=</span> <span class="va">tt</span><span class="op">)</span>
<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">'snail'</span>,<span class="st">'mushroom'</span>, <span class="st">'sunflower'</span>,
                          <span class="st">'evergreen_tree'</span>,<span class="st">'leaves'</span>, <span class="st">'green_salad'</span><span class="op">)</span>,
                group <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">'animal'</span>, <span class="st">'fungi'</span>, <span class="st">'flowering plant'</span>,
                          <span class="st">'conifers'</span>, <span class="st">'ferns'</span>, <span class="st">'mosses'</span><span class="op">)</span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span>, size<span class="op">=</span><span class="fl">1</span>, color<span class="op">=</span><span class="st">'firebrick'</span><span class="op">)</span> <span class="op">%&lt;+%</span> <span class="va">d</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">4.5</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">ylim</a></span><span class="op">(</span><span class="fl">0.5</span>, <span class="fl">6.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>parse<span class="op">=</span><span class="st">"emoji"</span>, size<span class="op">=</span><span class="fl">15</span>, vjust<span class="op">=</span><span class="fl">.25</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label <span class="op">=</span> <span class="va">group</span><span class="op">)</span>, geom<span class="op">=</span><span class="st">"label"</span>, x<span class="op">=</span><span class="fl">3.5</span>, hjust<span class="op">=</span><span class="fl">.5</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/52.png"></div>
</div>
<div id="emoji注释circularfan树" class="section level5" number="3.2.6.2.22">
<h5>
<span class="header-section-number">3.2.6.2.22</span> <code>Emoji</code>注释circular/fan树<a class="anchor" aria-label="anchor" href="#emoji%E6%B3%A8%E9%87%8Acircularfan%E6%A0%91"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb84"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout <span class="op">=</span> <span class="st">"circular"</span>, size<span class="op">=</span><span class="fl">1</span><span class="op">)</span> <span class="op">+</span>  
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>parse<span class="op">=</span><span class="st">"emoji"</span>, size<span class="op">=</span><span class="fl">10</span>, vjust<span class="op">=</span><span class="fl">.25</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">p</span><span class="op">)</span>

<span class="co">## fan layout  </span>
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/open_tree.html">open_tree</a></span><span class="op">(</span><span class="va">p</span>, angle<span class="op">=</span><span class="fl">200</span><span class="op">)</span> 
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">p2</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate_tree.html">rotate_tree</a></span><span class="op">(</span><span class="op">-</span><span class="fl">90</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/53.png"></div>
</div>
<div id="emoji注释clades" class="section level5" number="3.2.6.2.23">
<h5>
<span class="header-section-number">3.2.6.2.23</span> <code>Emoji</code>注释clades<a class="anchor" aria-label="anchor" href="#emoji%E6%B3%A8%E9%87%8Aclades"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb85"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op">(</span><span class="fl">123</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/rtree.html">rtree</a></span><span class="op">(</span><span class="fl">30</span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tr</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">5.2</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">41</span>, label<span class="op">=</span><span class="st">"chicken"</span>, parse<span class="op">=</span><span class="st">"emoji"</span>,
                    fontsize<span class="op">=</span><span class="fl">12</span>, align<span class="op">=</span><span class="cn">TRUE</span>, colour<span class="op">=</span><span class="st">"firebrick"</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">53</span>, label<span class="op">=</span><span class="st">"duck"</span>, parse<span class="op">=</span><span class="st">"emoji"</span>,
                    fontsize<span class="op">=</span><span class="fl">12</span>, align<span class="op">=</span><span class="cn">TRUE</span>, colour<span class="op">=</span><span class="st">"steelblue"</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelabel.html">geom_cladelabel</a></span><span class="op">(</span>node<span class="op">=</span><span class="fl">48</span>, label<span class="op">=</span><span class="st">"family"</span>, parse<span class="op">=</span><span class="st">"emoji"</span>,
                    fontsize<span class="op">=</span><span class="fl">12</span>, align<span class="op">=</span><span class="cn">TRUE</span>, colour<span class="op">=</span><span class="st">"darkkhaki"</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/54.png"></div>
</div>
<div id="applecoloremoji" class="section level5" number="3.2.6.2.24">
<h5>
<span class="header-section-number">3.2.6.2.24</span> <code>AppleColorEmoji</code><a class="anchor" aria-label="anchor" href="#applecoloremoji"><i class="fas fa-link"></i></a>
</h5>
<div class="sourceCode" id="cb86"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="va">tree_text</span> <span class="op">&lt;-</span> <span class="st">"(((((cow, (whale, dolphin)), (pig2, boar)), camel), fish), seedling);"</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span>text<span class="op">=</span><span class="va">tree_text</span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/GuangchuangYu/ggimage">ggimage</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va">gridSVG</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span>  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">x</span>, size<span class="op">=</span><span class="fl">2</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">20</span>, parse<span class="op">=</span><span class="st">'emoji'</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">xlim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">7</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/lims.html">ylim</a></span><span class="op">(</span><span class="cn">NA</span>, <span class="fl">8.5</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu"><a href="https://rdrr.io/pkg/ggimage/man/geom_phylopic.html">geom_phylopic</a></span><span class="op">(</span>image<span class="op">=</span><span class="st">"79ad5f09-cf21-4c89-8e7d-0c82a00ce728"</span>,
                  color<span class="op">=</span><span class="st">"firebrick"</span>, alpha <span class="op">=</span> <span class="fl">.3</span>, size<span class="op">=</span><span class="cn">Inf</span><span class="op">)</span>

<span class="va">p</span>
<span class="va">ps</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/gridSVG/man/grid.export.html">grid.export</a></span><span class="op">(</span><span class="st">"emoji.svg"</span>, addClass<span class="op">=</span><span class="cn">T</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/55.png"></div>
</div>
</div>
</div>
<div id="ggtree可视化其他数据" class="section level3" number="3.2.7">
<h3>
<span class="header-section-number">3.2.7</span> <code>ggtree</code>可视化其他数据<a class="anchor" aria-label="anchor" href="#ggtree%E5%8F%AF%E8%A7%86%E5%8C%96%E5%85%B6%E4%BB%96%E6%95%B0%E6%8D%AE"><i class="fas fa-link"></i></a>
</h3>
<p><code>ggtree</code>支持其他的数据类型，比如树状图（更多格式请移步Y叔博客）：</p>
<div class="sourceCode" id="cb87"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">hc</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/hclust.html">hclust</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/stats/dist.html">dist</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span><span class="op">)</span>
<span class="va">hc</span>
<span class="va">clus</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/cutree.html">cutree</a></span><span class="op">(</span><span class="va">hc</span>, <span class="fl">4</span><span class="op">)</span>
<span class="va">g</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/split.html">split</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">clus</span><span class="op">)</span>, <span class="va">clus</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">hc</span>, linetype<span class="op">=</span><span class="st">'dashed'</span><span class="op">)</span>
<span class="va">clades</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html">sapply</a></span><span class="op">(</span><span class="va">g</span>, <span class="kw">function</span><span class="op">(</span><span class="va">n</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/MRCA.html">MRCA</a></span><span class="op">(</span><span class="va">p</span>, <span class="va">n</span><span class="op">)</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupClade.html">groupClade</a></span><span class="op">(</span><span class="va">p</span>, <span class="va">clades</span>, group_name<span class="op">=</span><span class="st">'subtree'</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">subtree</span><span class="op">)</span>

<span class="va">d</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">clus</span><span class="op">)</span>, 
                cyl <span class="op">=</span> <span class="va">mtcars</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">clus</span><span class="op">)</span>, <span class="st">"cyl"</span><span class="op">]</span><span class="op">)</span>

<span class="va">p</span> <span class="op">%&lt;+%</span> <span class="va">d</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_dendrogram</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">5</span>, shape<span class="op">=</span><span class="fl">21</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">cyl</span><span class="op">)</span>, x<span class="op">=</span><span class="va">x</span><span class="op">+</span><span class="fl">.5</span><span class="op">)</span>, color<span class="op">=</span><span class="st">'black'</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>label<span class="op">=</span><span class="va">cyl</span><span class="op">)</span>, size<span class="op">=</span><span class="fl">3</span>, hjust<span class="op">=</span><span class="fl">.5</span>, color<span class="op">=</span><span class="st">'black'</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span>angle<span class="op">=</span><span class="fl">90</span>, hjust<span class="op">=</span><span class="fl">1</span>, offset<span class="op">=</span><span class="op">-</span><span class="fl">10</span>, show.legend<span class="op">=</span><span class="cn">F</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_brewer.html">scale_color_brewer</a></span><span class="op">(</span>palette<span class="op">=</span><span class="st">'Set1'</span>, breaks<span class="op">=</span><span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/theme_dendrogram.html">theme_dendrogram</a></span><span class="op">(</span>plot.margin<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">margin</a></span><span class="op">(</span><span class="fl">6</span>,<span class="fl">6</span>,<span class="fl">80</span>,<span class="fl">6</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.9</span>, <span class="fl">.6</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/56.png"></div>
</div>
<div id="ggtree拓展ggtreeextra" class="section level3" number="3.2.8">
<h3>
<span class="header-section-number">3.2.8</span> <code>ggtree</code>拓展<code>ggtreeExtra</code><a class="anchor" aria-label="anchor" href="#ggtree%E6%8B%93%E5%B1%95ggtreeextra"><i class="fas fa-link"></i></a>
</h3>
<p><code>ggtreeExtra</code>真的是惊艳到我了，只能献出我的膝盖啊！</p>
<p><code>ggtree</code>的函数<code><a href="https://rdrr.io/pkg/ggtree/man/facet-plot.html">geom_facet()</a></code>只支持<code>rectangular</code>、 <code>roundrect</code>、 <code>ellipse</code> 及 <code>slanted</code> 这4种输出样式，并不支持在<code>circular</code>、<code>fan</code>及<code>radial</code>这几种输出样式的外环上添加图层，为了解决这个问题，Y叔团队开发了新的R包：<code>ggtreeExtra</code>！</p>
<div id="添加微生物组丰度" class="section level4" number="3.2.8.1">
<h4>
<span class="header-section-number">3.2.8.1</span> 添加微生物组丰度<a class="anchor" aria-label="anchor" href="#%E6%B7%BB%E5%8A%A0%E5%BE%AE%E7%94%9F%E7%89%A9%E7%BB%84%E4%B8%B0%E5%BA%A6"><i class="fas fa-link"></i></a>
</h4>
<p><code>ggtree</code>直接支持<code>phyloseq</code>对象，这个对做微生物的来说，简直就是福音啊！</p>
<div class="sourceCode" id="cb88"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/ggtreeExtra/">ggtreeExtra</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="http://dx.plos.org/10.1371/journal.pone.0061217">phyloseq</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="st">"GlobalPatterns"</span><span class="op">)</span>
<span class="va">GP</span> <span class="op">&lt;-</span> <span class="va">GlobalPatterns</span>
<span class="va">GP</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/prune_taxa-methods.html">prune_taxa</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/taxa_sums.html">taxa_sums</a></span><span class="op">(</span><span class="va">GP</span><span class="op">)</span> <span class="op">&gt;</span> <span class="fl">600</span>, <span class="va">GP</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/sample_data-methods.html">sample_data</a></span><span class="op">(</span><span class="va">GP</span><span class="op">)</span><span class="op">$</span><span class="va">human</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/get_variable.html">get_variable</a></span><span class="op">(</span><span class="va">GP</span>, <span class="st">"SampleType"</span><span class="op">)</span> <span class="op">%in%</span>
                              <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Feces"</span>, <span class="st">"Skin"</span><span class="op">)</span>
<span class="va">mergedGP</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/merge_samples-methods.html">merge_samples</a></span><span class="op">(</span><span class="va">GP</span>, <span class="st">"SampleType"</span><span class="op">)</span>
<span class="va">mergedGP</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/rarefy_even_depth.html">rarefy_even_depth</a></span><span class="op">(</span><span class="va">mergedGP</span>,rngseed<span class="op">=</span><span class="fl">394582</span><span class="op">)</span>
<span class="va">mergedGP</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/tax_glom.html">tax_glom</a></span><span class="op">(</span><span class="va">mergedGP</span>,<span class="st">"Order"</span><span class="op">)</span>

<span class="va">melt_simple</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/phyloseq/man/psmelt.html">psmelt</a></span><span class="op">(</span><span class="va">mergedGP</span><span class="op">)</span> <span class="op">%&gt;%</span>
               <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">Abundance</span> <span class="op">&lt;</span> <span class="fl">120</span><span class="op">)</span> <span class="op">%&gt;%</span>
               <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="va">OTU</span>, val<span class="op">=</span><span class="va">Abundance</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">mergedGP</span>, layout<span class="op">=</span><span class="st">"fan"</span>, open.angle<span class="op">=</span><span class="fl">10</span><span class="op">)</span> <span class="op">+</span> 
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span>mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>color<span class="op">=</span><span class="va">Phylum</span><span class="op">)</span>, 
                   size<span class="op">=</span><span class="fl">1.5</span>,
                   show.legend<span class="op">=</span><span class="cn">FALSE</span><span class="op">)</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/rotate_tree.html">rotate_tree</a></span><span class="op">(</span><span class="va">p</span>, <span class="op">-</span><span class="fl">90</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>
         data<span class="op">=</span><span class="va">melt_simple</span>,
         geom<span class="op">=</span><span class="va">geom_boxplot</span>,
         mapping <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>
                     y<span class="op">=</span><span class="va">OTU</span>,
                     x<span class="op">=</span><span class="va">val</span>,
                     group<span class="op">=</span><span class="va">label</span>,
                     fill<span class="op">=</span><span class="va">Phylum</span>,
                   <span class="op">)</span>,
         size<span class="op">=</span><span class="fl">.2</span>,
         outlier.size<span class="op">=</span><span class="fl">0.5</span>,
         outlier.stroke<span class="op">=</span><span class="fl">0.08</span>,
         outlier.shape<span class="op">=</span><span class="fl">21</span>,
         axis.params<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>
                         axis       <span class="op">=</span> <span class="st">"x"</span>,
                         text.size  <span class="op">=</span> <span class="fl">1.8</span>,
                         hjust      <span class="op">=</span> <span class="fl">1</span>,
                         vjust      <span class="op">=</span> <span class="fl">0.5</span>,
                         nbreak     <span class="op">=</span> <span class="fl">3</span>,
                     <span class="op">)</span>,
         grid.params<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span><span class="op">)</span>
     <span class="op">)</span> 
     
<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_colour_discrete.html">scale_fill_discrete</a></span><span class="op">(</span>
         name<span class="op">=</span><span class="st">"Phyla"</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.8</span>, keyheight<span class="op">=</span><span class="fl">0.8</span>, ncol<span class="op">=</span><span class="fl">1</span><span class="op">)</span>
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>
         legend.title<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">9</span><span class="op">)</span>, <span class="co"># The title of legend </span>
         legend.text<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">7</span><span class="op">)</span> <span class="co"># The label text of legend, the sizes should be adjust with dpi.</span>
     <span class="op">)</span>
<span class="va">p</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/57.png"></div>
</div>
<div id="多维数据添加多个图层" class="section level4" number="3.2.8.2">
<h4>
<span class="header-section-number">3.2.8.2</span> 多维数据添加多个图层<a class="anchor" aria-label="anchor" href="#%E5%A4%9A%E7%BB%B4%E6%95%B0%E6%8D%AE%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E5%9B%BE%E5%B1%82"><i class="fas fa-link"></i></a>
</h4>
<div class="sourceCode" id="cb89"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/ggtreeExtra/">ggtreeExtra</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/xiangpin/ggstar/">ggstar</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va">ggnewscale</span><span class="op">)</span>

<span class="va">tree</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"data/HMP_tree/hmptree.nwk"</span><span class="op">)</span>
<span class="co"># the abundance and types of microbes</span>
<span class="va">dat1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"data/HMP_tree/tippoint_attr.csv"</span><span class="op">)</span>
<span class="co"># the abundance of microbes at different body sites.</span>
<span class="va">dat2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"data/HMP_tree/ringheatmap_attr.csv"</span><span class="op">)</span>
<span class="co"># the abundance of microbes at the body sites of greatest prevalence.</span>
<span class="va">dat3</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"data/HMP_tree/barplot_attr.csv"</span><span class="op">)</span>

<span class="co"># adjust the order</span>
<span class="va">dat2</span><span class="op">$</span><span class="va">Sites</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat2</span><span class="op">$</span><span class="va">Sites</span>, levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Stool (prevalence)"</span>, <span class="st">"Cheek (prevalence)"</span>,
                                          <span class="st">"Plaque (prevalence)"</span>,<span class="st">"Tongue (prevalence)"</span>,
                                          <span class="st">"Nose (prevalence)"</span>, <span class="st">"Vagina (prevalence)"</span>,
                                          <span class="st">"Skin (prevalence)"</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat3</span><span class="op">$</span><span class="va">Sites</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat3</span><span class="op">$</span><span class="va">Sites</span>, levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Stool (prevalence)"</span>, <span class="st">"Cheek (prevalence)"</span>,
                                          <span class="st">"Plaque (prevalence)"</span>, <span class="st">"Tongue (prevalence)"</span>,
                                          <span class="st">"Nose (prevalence)"</span>, <span class="st">"Vagina (prevalence)"</span>,
                                          <span class="st">"Skin (prevalence)"</span><span class="op">)</span><span class="op">)</span>
<span class="co"># extract the clade label information. Because some nodes of tree are annotated to genera,</span>
<span class="co"># which can be displayed with high light using ggtree.</span>
<span class="va">nodeids</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/nodeid.html">nodeid</a></span><span class="op">(</span><span class="va">tree</span>, <span class="va">tree</span><span class="op">$</span><span class="va">node.label</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op">(</span><span class="va">tree</span><span class="op">$</span><span class="va">node.label</span><span class="op">)</span><span class="op">&gt;</span><span class="fl">4</span><span class="op">]</span><span class="op">)</span>
<span class="va">nodedf</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>node<span class="op">=</span><span class="va">nodeids</span><span class="op">)</span>
<span class="va">nodelab</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/grep.html">gsub</a></span><span class="op">(</span><span class="st">"[\\.0-9]"</span>, <span class="st">""</span>, <span class="va">tree</span><span class="op">$</span><span class="va">node.label</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op">(</span><span class="va">tree</span><span class="op">$</span><span class="va">node.label</span><span class="op">)</span><span class="op">&gt;</span><span class="fl">4</span><span class="op">]</span><span class="op">)</span>
<span class="co"># The layers of clade and hightlight</span>
<span class="va">poslist</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">1.6</span>, <span class="fl">1.4</span>, <span class="fl">1.6</span>, <span class="fl">0.8</span>, <span class="fl">0.1</span>, <span class="fl">0.25</span>, <span class="fl">1.6</span>, <span class="fl">1.6</span>, <span class="fl">1.2</span>, <span class="fl">0.4</span>,
             <span class="fl">1.2</span>, <span class="fl">1.8</span>, <span class="fl">0.3</span>, <span class="fl">0.8</span>, <span class="fl">0.4</span>, <span class="fl">0.3</span>, <span class="fl">0.4</span>, <span class="fl">0.4</span>, <span class="fl">0.4</span>, <span class="fl">0.6</span>,
             <span class="fl">0.3</span>, <span class="fl">0.4</span>, <span class="fl">0.3</span><span class="op">)</span>
<span class="va">labdf</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>node<span class="op">=</span><span class="va">nodeids</span>, label<span class="op">=</span><span class="va">nodelab</span>, pos<span class="op">=</span><span class="va">poslist</span><span class="op">)</span>

<span class="co"># The circular layout tree.</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tree</span>, layout<span class="op">=</span><span class="st">"fan"</span>, size<span class="op">=</span><span class="fl">0.15</span>, open.angle<span class="op">=</span><span class="fl">5</span><span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom-hilight.html">geom_hilight</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">nodedf</span>, mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>node<span class="op">=</span><span class="va">node</span><span class="op">)</span>,
                  extendto<span class="op">=</span><span class="fl">6.8</span>, alpha<span class="op">=</span><span class="fl">0.3</span>, fill<span class="op">=</span><span class="st">"grey"</span>, color<span class="op">=</span><span class="st">"grey50"</span>,
                  size<span class="op">=</span><span class="fl">0.05</span><span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_cladelab.html">geom_cladelab</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">labdf</span>, 
                   mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>node<span class="op">=</span><span class="va">node</span>, 
                               label<span class="op">=</span><span class="va">label</span>,
                               offset.text<span class="op">=</span><span class="va">pos</span><span class="op">)</span>,
                   hjust<span class="op">=</span><span class="fl">0.5</span>,
                   angle<span class="op">=</span><span class="st">"auto"</span>,
                   barsize<span class="op">=</span><span class="cn">NA</span>,
                   horizontal<span class="op">=</span><span class="cn">FALSE</span>, 
                   fontsize<span class="op">=</span><span class="fl">1.4</span>,
                   fontface<span class="op">=</span><span class="st">"italic"</span>
                   <span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">%&lt;+%</span> <span class="va">dat1</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggstar/man/geom_star.html">geom_star</a></span><span class="op">(</span>
                        mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="va">Phylum</span>, starshape<span class="op">=</span><span class="va">Type</span>, size<span class="op">=</span><span class="va">Size</span><span class="op">)</span>,
                        position<span class="op">=</span><span class="st">"identity"</span>,starstroke<span class="op">=</span><span class="fl">0.1</span><span class="op">)</span> <span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"#FFC125"</span>,<span class="st">"#87CEFA"</span>,<span class="st">"#7B68EE"</span>,<span class="st">"#808080"</span>,<span class="st">"#800080"</span>,
                                    <span class="st">"#9ACD32"</span>,<span class="st">"#D15FEE"</span>,<span class="st">"#FFC0CB"</span>,<span class="st">"#EE6A50"</span>,<span class="st">"#8DEEEE"</span>,
                                    <span class="st">"#006400"</span>,<span class="st">"#800000"</span>,<span class="st">"#B0171F"</span>,<span class="st">"#191970"</span><span class="op">)</span>,
                           guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth <span class="op">=</span> <span class="fl">0.5</span>, keyheight <span class="op">=</span> <span class="fl">0.5</span>, order<span class="op">=</span><span class="fl">1</span>,
                                              override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>starshape<span class="op">=</span><span class="fl">15</span><span class="op">)</span><span class="op">)</span>,
                           na.translate<span class="op">=</span><span class="cn">FALSE</span><span class="op">)</span><span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggstar/man/scale_manual.html">scale_starshape_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">15</span>, <span class="fl">1</span><span class="op">)</span>,
                                guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth <span class="op">=</span> <span class="fl">0.5</span>, keyheight <span class="op">=</span> <span class="fl">0.5</span>, order<span class="op">=</span><span class="fl">2</span><span class="op">)</span>,
                                na.translate<span class="op">=</span><span class="cn">FALSE</span><span class="op">)</span><span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_size.html">scale_size_continuous</a></span><span class="op">(</span>range <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fl">2.5</span><span class="op">)</span>,
                               guide <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth <span class="op">=</span> <span class="fl">0.5</span>, keyheight <span class="op">=</span> <span class="fl">0.5</span>, order<span class="op">=</span><span class="fl">3</span>,
                                                    override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>starshape<span class="op">=</span><span class="fl">15</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
                                                    
<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggnewscale/man/new_scale.html">new_scale_fill</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">dat2</span>, geom<span class="op">=</span><span class="va">geom_tile</span>,
                    mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>y<span class="op">=</span><span class="va">ID</span>, x<span class="op">=</span><span class="va">Sites</span>, alpha<span class="op">=</span><span class="va">Abundance</span>, fill<span class="op">=</span><span class="va">Sites</span><span class="op">)</span>,
                    color <span class="op">=</span> <span class="st">"grey50"</span>, offset <span class="op">=</span> <span class="fl">0.04</span>,size <span class="op">=</span> <span class="fl">0.02</span><span class="op">)</span><span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_alpha.html">scale_alpha_continuous</a></span><span class="op">(</span>range<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span><span class="op">)</span>,
                             guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth <span class="op">=</span> <span class="fl">0.3</span>, keyheight <span class="op">=</span> <span class="fl">0.3</span>, order<span class="op">=</span><span class="fl">5</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>data<span class="op">=</span><span class="va">dat3</span>, geom<span class="op">=</span><span class="va">geom_bar</span>,
                    mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>y<span class="op">=</span><span class="va">ID</span>, x<span class="op">=</span><span class="va">HigherAbundance</span>, fill<span class="op">=</span><span class="va">Sites</span><span class="op">)</span>,
                    pwidth<span class="op">=</span><span class="fl">0.38</span>, 
                    orientation<span class="op">=</span><span class="st">"y"</span>, 
                    stat<span class="op">=</span><span class="st">"identity"</span>,
         <span class="op">)</span> <span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"#0000FF"</span>,<span class="st">"#FFA500"</span>,<span class="st">"#FF0000"</span>,<span class="st">"#800000"</span>,
                                    <span class="st">"#006400"</span>,<span class="st">"#800080"</span>,<span class="st">"#696969"</span><span class="op">)</span>,
                           guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth <span class="op">=</span> <span class="fl">0.3</span>, keyheight <span class="op">=</span> <span class="fl">0.3</span>, order<span class="op">=</span><span class="fl">4</span><span class="op">)</span><span class="op">)</span><span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_treescale.html">geom_treescale</a></span><span class="op">(</span>fontsize<span class="op">=</span><span class="fl">2</span>, linesize<span class="op">=</span><span class="fl">0.3</span>, x<span class="op">=</span><span class="fl">4.9</span>, y<span class="op">=</span><span class="fl">0.1</span><span class="op">)</span> <span class="op">+</span>
         <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0.93</span>, <span class="fl">0.5</span><span class="op">)</span>,
               legend.background<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_rect</a></span><span class="op">(</span>fill<span class="op">=</span><span class="cn">NA</span><span class="op">)</span>,
               legend.title<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">6.5</span><span class="op">)</span>,
               legend.text<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">4.5</span><span class="op">)</span>,
               legend.spacing.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.02</span>, <span class="st">"cm"</span><span class="op">)</span>,
             <span class="op">)</span>
<span class="va">p</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/58.png"></div>
<p>另外一种输出方式：</p>
<div class="sourceCode" id="cb90"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p</span> <span class="op">+</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/tree-layout.html">layout_rectangular</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
    <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>legend.position<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">.05</span>, <span class="fl">.7</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/59.png"></div>
</div>
</div>
<div id="群体遗学传例子" class="section level3" number="3.2.9">
<h3>
<span class="header-section-number">3.2.9</span> 群体遗学传例子<a class="anchor" aria-label="anchor" href="#%E7%BE%A4%E4%BD%93%E9%81%97%E5%AD%A6%E4%BC%A0%E4%BE%8B%E5%AD%90"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb91"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/ggtreeExtra/">ggtreeExtra</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va">ggnewscale</span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/xiangpin/ggstar/">ggstar</a></span><span class="op">)</span>

<span class="va">dat</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"data/microreact/Candida_auris/microreact-project-Candidaauris-data.csv"</span><span class="op">)</span>
<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"data/microreact/Candida_auris/microreact-project-Candidaauris-tree.nwk"</span><span class="op">)</span>

<span class="va">countries</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Canada"</span>, <span class="st">"United States"</span>,
               <span class="st">"Colombia"</span>, <span class="st">"Panama"</span>,
               <span class="st">"Venezuela"</span>, <span class="st">"France"</span>,
               <span class="st">"Germany"</span>, <span class="st">"Spain"</span>,
               <span class="st">"UK"</span>, <span class="st">"India"</span>,
               <span class="st">"Israel"</span>, <span class="st">"Pakistan"</span>,
               <span class="st">"Saudi Arabia"</span>, <span class="st">"United Arab Emirates"</span>,
               <span class="st">"Kenya"</span>, <span class="st">"South Africa"</span>,
               <span class="st">"Japan"</span>, <span class="st">"South Korea"</span>,
               <span class="st">"Australia"</span><span class="op">)</span>
<span class="co"># For the tip points</span>
<span class="va">dat1</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ID"</span>, <span class="st">"COUNTRY"</span>, <span class="st">"COUNTRY__colour"</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat1</span><span class="op">$</span><span class="va">COUNTRY</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat1</span><span class="op">$</span><span class="va">COUNTRY</span>, levels<span class="op">=</span><span class="va">countries</span><span class="op">)</span>
<span class="va">COUNTRYcolors</span> <span class="op">&lt;-</span> <span class="va">dat1</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/match.html">match</a></span><span class="op">(</span><span class="va">countries</span>,<span class="va">dat</span><span class="op">$</span><span class="va">COUNTRY</span><span class="op">)</span>,<span class="st">"COUNTRY__colour"</span><span class="op">]</span>

<span class="co"># For the heatmap layer</span>
<span class="va">dat2</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ID"</span>, <span class="st">"FCZ"</span>, <span class="st">"AMB"</span>, <span class="st">"MCF"</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat2</span> <span class="op">&lt;-</span> <span class="fu">reshape2</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/reshape2/man/melt.html">melt</a></span><span class="op">(</span><span class="va">dat2</span>,id<span class="op">=</span><span class="st">"ID"</span>, variable.name<span class="op">=</span><span class="st">"Antifungal"</span>, value.name<span class="op">=</span><span class="st">"type"</span><span class="op">)</span>
<span class="va">dat2</span><span class="op">$</span><span class="va">type</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="va">dat2</span><span class="op">$</span><span class="va">Antifungal</span>, <span class="va">dat2</span><span class="op">$</span><span class="va">type</span><span class="op">)</span>
<span class="va">dat2</span><span class="op">$</span><span class="va">type</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">dat2</span><span class="op">$</span><span class="va">type</span>,
                           <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="fu"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/grep.html">grepl</a></span><span class="op">(</span><span class="st">"Not_"</span>, <span class="va">x</span><span class="op">)</span>, <span class="st">"Susceptible"</span>, <span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat2</span><span class="op">$</span><span class="va">Antifungal</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat2</span><span class="op">$</span><span class="va">Antifungal</span>, levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"FCZ"</span>, <span class="st">"AMB"</span>, <span class="st">"MCF"</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat2</span><span class="op">$</span><span class="va">type</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat2</span><span class="op">$</span><span class="va">type</span>,
                    levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"FCZ Resistant"</span>,
                            <span class="st">"AMB Resistant"</span>,
                            <span class="st">"MCF Resistant"</span>,
                            <span class="st">"Susceptible"</span><span class="op">)</span><span class="op">)</span>

<span class="co"># For the points layer</span>
<span class="va">dat3</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ID"</span>, <span class="st">"ERG11"</span>, <span class="st">"FKS1"</span><span class="op">)</span><span class="op">)</span> <span class="op">%&gt;%</span>
        <span class="fu">reshape2</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/reshape2/man/melt.html">melt</a></span><span class="op">(</span>id<span class="op">=</span><span class="st">"ID"</span>, variable.name<span class="op">=</span><span class="st">"point"</span>, value.name<span class="op">=</span><span class="st">"mutation"</span><span class="op">)</span>
<span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="va">dat3</span><span class="op">$</span><span class="va">point</span>, <span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span><span class="op">)</span>
<span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span>, <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="fu"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/grep.html">grepl</a></span><span class="op">(</span><span class="st">"WT"</span>,<span class="va">x</span><span class="op">)</span>, <span class="cn">NA</span>,<span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">dat3</span><span class="op">$</span><span class="va">mutation</span>, levels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ERG11 Y132F"</span>, <span class="st">"ERG11 K143R"</span>,
                                                <span class="st">"ERG11 F126L"</span>, <span class="st">"FKS1 S639Y/P/F"</span><span class="op">)</span><span class="op">)</span>

<span class="co"># For the clade group</span>
<span class="va">dat4</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ID"</span>, <span class="st">"CLADE"</span><span class="op">)</span><span class="op">)</span>
<span class="va">dat4</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/stats/aggregate.html">aggregate</a></span><span class="op">(</span><span class="va">.</span><span class="op">~</span><span class="va">CLADE</span>, <span class="va">dat4</span>, FUN<span class="op">=</span><span class="va">paste</span>, collapse<span class="op">=</span><span class="st">","</span><span class="op">)</span>
<span class="va">clades</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">dat4</span><span class="op">$</span><span class="va">ID</span>, <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">{</span><span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/strsplit.html">strsplit</a></span><span class="op">(</span><span class="va">x</span>,split<span class="op">=</span><span class="st">","</span><span class="op">)</span><span class="op">)</span><span class="op">}</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="va">clades</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">dat4</span><span class="op">$</span><span class="va">CLADE</span>

<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/tidytree/man/groupOTU.html">groupOTU</a></span><span class="op">(</span><span class="va">tr</span>, <span class="va">clades</span>, <span class="st">"Clade"</span><span class="op">)</span>
<span class="va">Clade</span> <span class="op">&lt;-</span> <span class="cn">NULL</span>
<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span>tr<span class="op">=</span><span class="va">tr</span>, layout<span class="op">=</span><span class="st">"fan"</span>, open.angle<span class="op">=</span><span class="fl">15</span>, size<span class="op">=</span><span class="fl">0.2</span>, <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>colour<span class="op">=</span><span class="va">Clade</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_colour_manual</a></span><span class="op">(</span>
         values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"black"</span>,<span class="st">"#69B920"</span>,<span class="st">"#9C2E88"</span>,<span class="st">"#F74B00"</span>,<span class="st">"#60C3DB"</span><span class="op">)</span>,
         labels<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">""</span>,<span class="st">"I"</span>, <span class="st">"II"</span>, <span class="st">"III"</span>, <span class="st">"IV"</span><span class="op">)</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.5</span>,
                            keyheight<span class="op">=</span><span class="fl">0.5</span>,
                            order<span class="op">=</span><span class="fl">1</span>,
                            override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>linetype<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"0"</span><span class="op">=</span><span class="cn">NA</span>,
                                                         <span class="st">"Clade1"</span><span class="op">=</span><span class="fl">1</span>,
                                                         <span class="st">"Clade2"</span><span class="op">=</span><span class="fl">1</span>,
                                                         <span class="st">"Clade3"</span><span class="op">=</span><span class="fl">1</span>,
                                                         <span class="st">"Clade4"</span><span class="op">=</span><span class="fl">1</span>
                                                        <span class="op">)</span>
                                             <span class="op">)</span>
                           <span class="op">)</span>
     <span class="op">)</span> <span class="op">+</span> 
     <span class="fu"><a href="https://rdrr.io/pkg/ggnewscale/man/new_scale.html">new_scale_colour</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">%&lt;+%</span> <span class="va">dat1</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>colour<span class="op">=</span><span class="va">COUNTRY</span><span class="op">)</span>,
                   alpha<span class="op">=</span><span class="fl">0</span><span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>colour<span class="op">=</span><span class="va">COUNTRY</span><span class="op">)</span>,
                   align<span class="op">=</span><span class="cn">TRUE</span>,
                   linetype<span class="op">=</span><span class="fl">3</span>,
                   size<span class="op">=</span><span class="fl">1</span>,
                   linesize<span class="op">=</span><span class="fl">0.2</span>,
                   show.legend<span class="op">=</span><span class="cn">FALSE</span>
                   <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_colour_manual</a></span><span class="op">(</span>
         name<span class="op">=</span><span class="st">"Country labels"</span>,
         values<span class="op">=</span><span class="va">COUNTRYcolors</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.5</span>,
                            keyheight<span class="op">=</span><span class="fl">0.5</span>,
                            order<span class="op">=</span><span class="fl">2</span>,
                            override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">2</span>,alpha<span class="op">=</span><span class="fl">1</span><span class="op">)</span><span class="op">)</span>
     <span class="op">)</span>

<span class="va">p2</span> <span class="op">&lt;-</span> <span class="va">p1</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>
          data<span class="op">=</span><span class="va">dat2</span>,
          geom<span class="op">=</span><span class="va">geom_tile</span>,
          mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">Antifungal</span>, y<span class="op">=</span><span class="va">ID</span>, fill<span class="op">=</span><span class="va">type</span><span class="op">)</span>,
          width<span class="op">=</span><span class="fl">0.1</span>,
          color<span class="op">=</span><span class="st">"white"</span>,
          pwidth<span class="op">=</span><span class="fl">0.1</span>,
          offset<span class="op">=</span><span class="fl">0.15</span>
      <span class="op">)</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>
           name<span class="op">=</span><span class="st">"Antifungal susceptibility"</span>,
           values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"#595959"</span>, <span class="st">"#B30000"</span>, <span class="st">"#020099"</span>, <span class="st">"#E6E6E6"</span><span class="op">)</span>,
           na.translate<span class="op">=</span><span class="cn">FALSE</span>,
           guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.5</span>,
                              keyheight<span class="op">=</span><span class="fl">0.5</span>,
                              order<span class="op">=</span><span class="fl">3</span>
                             <span class="op">)</span>
      <span class="op">)</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggnewscale/man/new_scale.html">new_scale_fill</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p3</span> <span class="op">&lt;-</span> <span class="va">p2</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>
          data<span class="op">=</span><span class="va">dat3</span>,
          geom<span class="op">=</span><span class="va">geom_star</span>,
          mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>x<span class="op">=</span><span class="va">mutation</span>, y<span class="op">=</span><span class="va">ID</span>, fill<span class="op">=</span><span class="va">mutation</span>, starshape<span class="op">=</span><span class="va">point</span><span class="op">)</span>,
          size<span class="op">=</span><span class="fl">1</span>,
          starstroke<span class="op">=</span><span class="fl">0</span>,
          pwidth<span class="op">=</span><span class="fl">0.1</span>,
          inherit.aes <span class="op">=</span> <span class="cn">FALSE</span>,
          grid.params<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>
                          linetype<span class="op">=</span><span class="fl">3</span>,
                          size<span class="op">=</span><span class="fl">0.2</span>
                      <span class="op">)</span>

      <span class="op">)</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>
          name<span class="op">=</span><span class="st">"Point mutations"</span>,
          values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"#329901"</span>, <span class="st">"#0600FF"</span>, <span class="st">"#FF0100"</span>, <span class="st">"#9900CC"</span><span class="op">)</span>,
          guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.5</span>, keyheight<span class="op">=</span><span class="fl">0.5</span>, order<span class="op">=</span><span class="fl">4</span>,
                             override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>starshape<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ERG11 Y132F"</span><span class="op">=</span><span class="fl">15</span>,
                                                           <span class="st">"ERG11 K143R"</span><span class="op">=</span><span class="fl">15</span>,
                                                           <span class="st">"ERG11 F126L"</span><span class="op">=</span><span class="fl">15</span>,
                                                           <span class="st">"FKS1 S639Y/P/F"</span><span class="op">=</span><span class="fl">1</span><span class="op">)</span>,
                                               size<span class="op">=</span><span class="fl">2</span><span class="op">)</span>
                            <span class="op">)</span>,
          na.translate<span class="op">=</span><span class="cn">FALSE</span>,
      <span class="op">)</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggstar/man/scale_manual.html">scale_starshape_manual</a></span><span class="op">(</span>
          values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">15</span>, <span class="fl">1</span><span class="op">)</span>,
          guide<span class="op">=</span><span class="st">"none"</span>
      <span class="op">)</span> <span class="op">+</span>
      <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>
          legend.background<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_rect</a></span><span class="op">(</span>fill<span class="op">=</span><span class="cn">NA</span><span class="op">)</span>,
          legend.title<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">7</span><span class="op">)</span>, <span class="co"># The size should be adjusted with different devout.</span>
          legend.text<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">5.5</span><span class="op">)</span>,
          legend.spacing.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.02</span>, <span class="st">"cm"</span><span class="op">)</span>
      <span class="op">)</span>
<span class="va">p3</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/60.png"></div>
<div class="sourceCode" id="cb92"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/ggtreeExtra/">ggtreeExtra</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">ggtree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va">ggnewscale</span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/YuLab-SMU/treeio">treeio</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://yulab-smu.top/treedata-book/">tidytree</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/xiangpin/ggstar/">ggstar</a></span><span class="op">)</span>

<span class="va">tr</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="st">"data/microreact/Salmonella_Typhi/microreact-project-NJIDqgsS-tree.nwk"</span><span class="op">)</span>

<span class="va">metada</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="st">"data/microreact/Salmonella_Typhi/microreact-project-NJIDqgsS-data.csv"</span><span class="op">)</span>

<span class="va">metadata</span> <span class="op">&lt;-</span> <span class="va">metada</span> <span class="op">%&gt;%</span>
            <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"id"</span>, <span class="st">"country"</span>, <span class="st">"country__colour"</span>, <span class="st">"year"</span>, <span class="st">"year__colour"</span>, <span class="st">"haplotype"</span><span class="op">)</span><span class="op">)</span>
<span class="va">metadata</span><span class="op">$</span><span class="va">haplotype</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><span class="op">(</span><span class="va">metadata</span><span class="op">$</span><span class="va">haplotype</span>, <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="fu"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span><span class="op">&gt;</span><span class="fl">0</span>,<span class="va">x</span>,<span class="cn">NA</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>

<span class="va">countrycolors</span> <span class="op">&lt;-</span> <span class="va">metada</span> <span class="op">%&gt;%</span>
                 <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"country"</span>, <span class="st">"country__colour"</span><span class="op">)</span><span class="op">)</span> <span class="op">%&gt;%</span>
                 <span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html">distinct</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">yearcolors</span> <span class="op">&lt;-</span> <span class="va">metada</span> <span class="op">%&gt;%</span>
              <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"year"</span>, <span class="st">"year__colour"</span><span class="op">)</span><span class="op">)</span> <span class="op">%&gt;%</span>
              <span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html">distinct</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">yearcolors</span> <span class="op">&lt;-</span> <span class="va">yearcolors</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/order.html">order</a></span><span class="op">(</span><span class="va">yearcolors</span><span class="op">$</span><span class="va">year</span>, decreasing<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span>,<span class="op">]</span>

<span class="va">metadata</span><span class="op">$</span><span class="va">country</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">metadata</span><span class="op">$</span><span class="va">country</span>, levels<span class="op">=</span><span class="va">countrycolors</span><span class="op">$</span><span class="va">country</span><span class="op">)</span>
<span class="va">metadata</span><span class="op">$</span><span class="va">year</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">metadata</span><span class="op">$</span><span class="va">year</span>, levels<span class="op">=</span><span class="va">yearcolors</span><span class="op">$</span><span class="va">year</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="va">tr</span>, layout<span class="op">=</span><span class="st">"fan"</span>, open.angle<span class="op">=</span><span class="fl">15</span>, size<span class="op">=</span><span class="fl">0.1</span><span class="op">)</span>

<span class="va">p</span> <span class="op">&lt;-</span> <span class="va">p</span> <span class="op">%&lt;+%</span> <span class="va">metadata</span>

<span class="va">p1</span> <span class="op">&lt;-</span><span class="va">p</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tippoint.html">geom_tippoint</a></span><span class="op">(</span>
         mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>colour<span class="op">=</span><span class="va">country</span><span class="op">)</span>,
         size<span class="op">=</span><span class="fl">1.5</span>,
         stroke<span class="op">=</span><span class="fl">0</span>,
         alpha<span class="op">=</span><span class="fl">0.4</span>
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_colour_manual</a></span><span class="op">(</span>
         name<span class="op">=</span><span class="st">"Country"</span>,
         values<span class="op">=</span><span class="va">countrycolors</span><span class="op">$</span><span class="va">country__colour</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.3</span>,
                            keyheight<span class="op">=</span><span class="fl">0.3</span>,
                            ncol<span class="op">=</span><span class="fl">2</span>,
                            override.aes<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">2</span>,alpha<span class="op">=</span><span class="fl">1</span><span class="op">)</span>,
                            order<span class="op">=</span><span class="fl">1</span><span class="op">)</span>
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>
         legend.title<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">5</span><span class="op">)</span>,
         legend.text<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">4</span><span class="op">)</span>,
         legend.spacing.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.02</span>, <span class="st">"cm"</span><span class="op">)</span>
     <span class="op">)</span>

<span class="va">p2</span> <span class="op">&lt;-</span><span class="va">p1</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>
         geom<span class="op">=</span><span class="va">geom_star</span>,
         mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="va">haplotype</span><span class="op">)</span>,
         starshape<span class="op">=</span><span class="fl">26</span>,
         color<span class="op">=</span><span class="cn">NA</span>,
         size<span class="op">=</span><span class="fl">2</span>,
         starstroke<span class="op">=</span><span class="fl">0</span>,
         offset<span class="op">=</span><span class="fl">0</span>,
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>
         name<span class="op">=</span><span class="st">"Haplotype"</span>,
         values<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"red"</span><span class="op">)</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>
                   keywidth<span class="op">=</span><span class="fl">0.3</span>,
                   keyheight<span class="op">=</span><span class="fl">0.3</span>,
                   order<span class="op">=</span><span class="fl">3</span>
               <span class="op">)</span>,
         na.translate<span class="op">=</span><span class="cn">FALSE</span>
     <span class="op">)</span>

<span class="va">p3</span> <span class="op">&lt;-</span><span class="va">p2</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggnewscale/man/new_scale.html">new_scale_fill</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggtreeExtra/man/geom_fruit.html">geom_fruit</a></span><span class="op">(</span>
         geom<span class="op">=</span><span class="va">geom_tile</span>,
         mapping<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/aes.html">aes</a></span><span class="op">(</span>fill<span class="op">=</span><span class="va">year</span><span class="op">)</span>,
         width<span class="op">=</span><span class="fl">0.002</span>,
         offset<span class="op">=</span><span class="fl">0.1</span>
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/scale_manual.html">scale_fill_manual</a></span><span class="op">(</span>
         name<span class="op">=</span><span class="st">"Year"</span>,
         values<span class="op">=</span><span class="va">yearcolors</span><span class="op">$</span><span class="va">year__colour</span>,
         guide<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/guide_legend.html">guide_legend</a></span><span class="op">(</span>keywidth<span class="op">=</span><span class="fl">0.3</span>, keyheight<span class="op">=</span><span class="fl">0.3</span>, ncol<span class="op">=</span><span class="fl">2</span>, order<span class="op">=</span><span class="fl">2</span><span class="op">)</span>
     <span class="op">)</span> <span class="op">+</span>
     <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/theme.html">theme</a></span><span class="op">(</span>
           legend.title<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">6</span><span class="op">)</span>, <span class="co"># The size should be adjusted with the different devout.</span>
           legend.text<span class="op">=</span><span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/element.html">element_text</a></span><span class="op">(</span>size<span class="op">=</span><span class="fl">4.5</span><span class="op">)</span>,
           legend.spacing.y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/grid/unit.html">unit</a></span><span class="op">(</span><span class="fl">0.02</span>, <span class="st">"cm"</span><span class="op">)</span>
           <span class="op">)</span>
<span class="va">p3</span></code></pre></div>
<div class="inline-figure"><img src="figures/ggtreeLearnNote/61.png"></div>

</div>
</div>
</div>
  <div class="chapter-nav">
<div class="prev"><a href="r-stat.html"><span class="header-section-number">2</span> 统计分析</a></div>
<div class="next"><a href="r-deve.html"><span class="header-section-number">4</span> R语言开发</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
    <nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
      <ul class="nav navbar-nav">
<li><a class="nav-link" href="#r-vis"><span class="header-section-number">3</span> 数据可视化</a></li>
<li>
<a class="nav-link" href="#ggplot2%E7%BB%98%E5%9B%BE%E7%9B%B8%E5%85%B3"><span class="header-section-number">3.1</span> ggplot2绘图相关</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#ggplot2%E7%BB%98%E5%88%B6%E6%A1%91%E5%9F%BA%E5%86%B2%E5%87%BB%E5%9B%BE"><span class="header-section-number">3.1.1</span> ggplot2绘制桑基(冲击图)</a></li>
<li><a class="nav-link" href="#%E5%85%B6%E4%BB%96tips"><span class="header-section-number">3.1.2</span> 其他Tips</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#ggtree%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"><span class="header-section-number">3.2</span> ggtree学习笔记</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2"><span class="header-section-number">3.2.1</span> 写在前面</a></li>
<li><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E8%BF%9B%E5%8C%96%E6%A0%91"><span class="header-section-number">3.2.2</span> 关于进化树</a></li>
<li><a class="nav-link" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F"><span class="header-section-number">3.2.3</span> 进化树数据格式</a></li>
<li><a class="nav-link" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86"><span class="header-section-number">3.2.4</span> 进化树数据处理</a></li>
<li><a class="nav-link" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E5%8F%AF%E8%A7%86%E5%8C%96"><span class="header-section-number">3.2.5</span> 进化树可视化</a></li>
<li><a class="nav-link" href="#%E8%BF%9B%E5%8C%96%E6%A0%91%E6%B3%A8%E9%87%8A"><span class="header-section-number">3.2.6</span> 进化树注释</a></li>
<li><a class="nav-link" href="#ggtree%E5%8F%AF%E8%A7%86%E5%8C%96%E5%85%B6%E4%BB%96%E6%95%B0%E6%8D%AE"><span class="header-section-number">3.2.7</span> ggtree可视化其他数据</a></li>
<li><a class="nav-link" href="#ggtree%E6%8B%93%E5%B1%95ggtreeextra"><span class="header-section-number">3.2.8</span> ggtree拓展ggtreeExtra</a></li>
<li><a class="nav-link" href="#%E7%BE%A4%E4%BD%93%E9%81%97%E5%AD%A6%E4%BC%A0%E4%BE%8B%E5%AD%90"><span class="header-section-number">3.2.9</span> 群体遗学传例子</a></li>
</ul>
</li>
</ul>

      <div class="book-extra">
        <ul class="list-unstyled">
<li><a id="book-source" href="https://github.com/lixiang117423/book4xiang/blob/master/3.R-vis.Rmd">View source <i class="fab fa-github"></i></a></li>
          <li><a id="book-edit" href="https://github.com/lixiang117423/book4xiang/edit/master/3.R-vis.Rmd">Edit this page <i class="fab fa-github"></i></a></li>
        </ul>
</div>
    </nav>
</div>

</div>
</div> <!-- .container -->

<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

  <div class="col-12 col-md-6 mt-3">
    <p>"<strong>小蓝哥的知识荒原</strong>" was written by 李详. It was last built on 2021年10月1日.</p>
  </div>

  <div class="col-12 col-md-6 mt-3">
    <p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
  </div>

</div></div>
</footer><!-- dynamically load mathjax for compatibility with self-contained --><script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script><script type="text/x-mathjax-config">const popovers = document.querySelectorAll('a.footnote-ref[data-toggle="popover"]');
for (let popover of popovers) {
  const div = document.createElement('div');
  div.setAttribute('style', 'position: absolute; top: 0, left:0; width:0, height:0, overflow: hidden; visibility: hidden;');
  div.innerHTML = popover.getAttribute('data-content');

  var has_math = div.querySelector("span.math");
  if (has_math) {
    document.body.appendChild(div);
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, div]);
    MathJax.Hub.Queue(function() {
      popover.setAttribute('data-content', div.innerHTML);
      document.body.removeChild(div);
    })
  }
}
</script>
</body>
</html>
